apache 如何使用 htaccess 将非 www 重定向到 www URL?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1157255/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to redirect non-www to www URL's using htaccess?
提问by Prashant
I have a website say http://www.example.com/in the root of my website, I have added .htaccess file to redirect any request of http://example.com/to http://www.example.com/
我在我的网站的根目录中有一个网站说http://www.example.com/,我添加了 .htaccess 文件以将http://example.com/ 的任何请求重定向到http://www.example。 com/
Recently I have created a new section "Videos" so the URL for videos is http://www.example.com/videos/. In this video folder I have another htaccess file which is performing rewriting for video entries. When I am trying to access http://example.com/videos/then its not redirecting me to http://www.example.com/videos/
最近我创建了一个新的“视频”部分,所以视频的 URL 是http://www.example.com/videos/。在这个视频文件夹中,我有另一个 htaccess 文件,它正在为视频条目执行重写。当我尝试访问http://example.com/videos/ 时,它不会将我重定向到http://www.example.com/videos/
I think .htacces is not inheriting the previous rules from the parent directory. Can anyone please tell me what can be the rule I can add in the .htaccess file of /videos/ folder so that any request for http://example.com/videos/will be redirected to http://www.example.com/videos/URL.
我认为 .htacces 没有从父目录继承以前的规则。任何人都可以告诉我我可以在 /videos/ 文件夹的 .htaccess 文件中添加什么规则,以便对http://example.com/videos/ 的任何请求都将被重定向到http://www.example。 com/videos/URL。
回答by Rodrigo De Almeida Siqueira
This is a more generic solution, because it can be used with any domain name without having to specify the specific domain name in each .htaccess:
这是一个更通用的解决方案,因为它可以与任何域名一起使用,而无需在每个 .htaccess 中指定特定的域名:
# Redirect non-www to www:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/ [R=301,L]
The contrary is also possible (www to non-www):
相反也是可能的(从 www 到非 www):
# Redirect www to non-www
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://%1/ [R=301,L]
回答by Laurence Gonsalves
I think it may just be that your existing rule is too strict, and is not getting triggered in your subdirectory because of this. Something like this should work site-wide:
我认为这可能只是您现有的规则过于严格,因此没有在您的子目录中触发。像这样的事情应该在整个站点范围内工作:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule ^(.*) http://www.example.com/ [R=301]
回答by Efforts Unlimited
This is Best Solutions to redirect non-www to www URL's using htaccess. using this code in htaccess file and check url.
这是使用 htaccess 将非 www 重定向到 www URL 的最佳解决方案。在 htaccess 文件中使用此代码并检查 url。
Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^mydomain.com [nc]
rewriterule ^(.*)$ http://www.mydomain.com/ [r=301,nc]
回答by shaz3e
I recommend everyone to stick with the below method it will only redirect the main domain only not any other sub-directory or sub-domain in your host.
我建议大家坚持使用以下方法,它只会重定向主域,而不会重定向主机中的任何其他子目录或子域。
# Redirect non-www to www only for main domain
# recommended if you have any/some sub-domain(s)
RewriteEngine on
RewriteBase /
# Replace yoursite and .tld respectively
RewriteCond %{HTTP_HOST} ^yoursite\.tld$
# Replace yoursite.com
RewriteRule ^(.*) http://www.yoursite.com/ [R=301]
Use this method if you are really sure and I hope you will that you won't ever use sub-domain with your website i.e. subdomain.yoursite.com or whatever. Than you're welcome to use the below method. "Saying Again make sure if you really want to use this method"
如果您真的确定,请使用此方法,我希望您永远不会在您的网站上使用子域,即 subdomain.yoursite.com 或其他任何内容。欢迎您使用以下方法。“再说一遍,确定你是否真的想使用这种方法”
# Redirect all non-www to www including subdomain(s)
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/ [R=301,L]
The below method will redirect all www url to non-www including your sub-domains although you cannot use www.subdirecty.yoursite.com it will prompt you an error with 500.
尽管您不能使用 www.subdirecty.yoursite.com,但以下方法会将所有 www url 重定向到非 www 网址,包括您的子域,但它会提示您 500 错误。
# Redirect www to non-www
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://%1/ [R=301,L]

