Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Apache 在my.htaccess中,我只想在url中没有子域的情况下重写url_Apache_.htaccess_Mod Rewrite_Url Rewriting - Fatal编程技术网

Apache 在my.htaccess中,我只想在url中没有子域的情况下重写url

Apache 在my.htaccess中,我只想在url中没有子域的情况下重写url,apache,.htaccess,mod-rewrite,url-rewriting,Apache,.htaccess,Mod Rewrite,Url Rewriting,我希望example.com重写为www.example.com,但仅当url中没有子域时: example.com -> rewrite to www.example.com s.example.com -> no rewrite peace.example.com -> no rewite 等等。。 等等 我发现的所有子域重写都添加了www,不管它是否有子域,这让我很困惑。我不希望它重定向到www.c.example.com您可以在文档的根目录下使用此代码 Rewri

我希望example.com重写为www.example.com,但仅当url中没有子域时:

example.com -> rewrite to www.example.com

s.example.com -> no rewrite

peace.example.com -> no rewite
等等。。 等等


我发现的所有子域重写都添加了www,不管它是否有子域,这让我很困惑。我不希望它重定向到www.c.example.com

您可以在
文档的根目录下使用此代码

RewriteEngine On

RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L]
标志:

  • NE
    -无编码
  • L
    -最后一条规则
  • R=301
    -使用http状态=301重定向
RewriteCond%{HTTP\u HOST}^example\.com$
将使其在域为
example.com
时执行

参考资料:

您可以使用两个虚拟主机,并为每个虚拟主机定义不同的重写规则

<VirtualHost *:80>
ServerName example.com
# rewrite to www.example.com
</VirtualHost>

<VirtualHost *:80>
ServerName *.example.com
# do not rewrite
</VirtualHost>

ServerName example.com
#重写到www.example.com
ServerName*.example.com
#不要重写
编辑:


但这需要使用虚拟主机文件。

Hi@anubhava可以解释这是怎么回事,因为我以前从未见过[NE]和[L]做什么?我有wordpress,它是否可能过度统治htaccess,因为它仍在将www.添加到我的子域(如www.c.example.com)中如果它是WP,那么它是1。确保这是
行中
重写引擎下面的第一条规则。2) 另外,请确保您的网站和主页url在permalinks中设置为
www.example.com