301将非www重定向到www并非始终有效 [英] 301 redirect non-www to www not always working
问题描述
我已经阅读了大量的帖子和页面,试图弄清楚这一点.我大部分都在工作.我设置了.htaccess文件,并且尝试将网站中的任何页面从非www重定向(301)重定向到www版本.
I've read through a ton of posts and pages trying to figure this out. I have it mostly working. I have a .htaccess file setup and I'm trying to redirect (301) any page in my site from non-www to www version.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
它似乎可以从基本url开始工作.但是,它不会重定向子页面.
It seems to work from the base url. However, it doesn't redirect for sub pages.
这有效:
example.com-> www.example.com
example.com -> www.example.com
这不起作用:
example.com/foo.html-> www.example.com/foo.html
example.com/foo.html -> www.example.com/foo.html
任何帮助将不胜感激
推荐答案
我认为您需要添加\
以逃避域中的点(第2行).
I think you need to add a \
to escape the dot in your domain (line 2).
如:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(.*)\.yourdomain\.com$ [NC]
RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [R=301,L]
...as seen here: http://www.hostingdiscussion.com/promotion-marketing/26083-301-redirect-non-www-www-vice-versa-good-search-engine-optimization-technique.html
这篇关于301将非www重定向到www并非始终有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!