Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/8.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 正确的htaccess 301以防止重复的主机名问题?_Apache_.htaccess_Mod Rewrite_Redirect_Hostname - Fatal编程技术网

Apache 正确的htaccess 301以防止重复的主机名问题?

Apache 正确的htaccess 301以防止重复的主机名问题?,apache,.htaccess,mod-rewrite,redirect,hostname,Apache,.htaccess,Mod Rewrite,Redirect,Hostname,我想确保没有重复的主机名问题,Google Analytics建议有 所以,我想确保我的主页始终是www.example.com,而不是example.com或者我想 这是如何安全完成的 我找到了一个例子,但由于我不是这方面的大师,我想我需要第二个或两个意见: RewriteEngine on RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$ RewriteRule .? http://www.example.com%{REQUEST_U

我想确保没有重复的主机名问题,Google Analytics建议有

所以,我想确保我的主页始终是www.example.com,而不是example.com或者我想

这是如何安全完成的

我找到了一个例子,但由于我不是这方面的大师,我想我需要第二个或两个意见:

 RewriteEngine on 
 RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$ 
 RewriteRule .? http://www.example.com%{REQUEST_URI} [R=301,L] 

解释得越多越好

您在
RewriteCond
中的正则表达式看起来可疑,因为它使整个主机名都是可选的。您可以改为使用此规则:

RewriteEngine on 

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