[英]How redirect .htaccess to www. version? (not working)
I have the following code in .htaccess
. 我在
.htaccess
有以下代码。 It doesn't seem to work for some reason. 它似乎不是出于某种原因而起作用。 Can anyone see why?
谁能明白为什么? It's in the root of my website.
它位于我网站的根目录中。
Options +FollowSymLinks
RewriteEngine On
#Limit redirects at once (stops server crash)
RewriteOptions MaxRedirects=10
#Redirect visitors to www. version of website
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.MYDOMAIN.COM/$1 [R=301]
Obviously, mydomain.com
is my domain name. 显然,
mydomain.com
是我的域名。 Thanks in advance to anyone who can spot any problems! 提前感谢能够发现任何问题的人!
UPDATE: 更新:
This works no problems. 这没有问题。 But if I access
mydomain.com/link1/link2/
then it does not work. 但是,如果我访问
mydomain.com/link1/link2/
然后它不起作用。
The .htaccess for '/link2/' is: '/ link2 /'的.htaccess是:
#settings
Options +FollowSymLinks
RewriteEngine On
#homepage
DirectoryIndex index.php
And for '/link1/': 对于'/ link1 /':
#/linkgenerator/ never used, but temp redirect for neatness
Redirect 302 /fbwiki/linkgenerator/ /fbwiki/link-generator/
#Serve error pages
ErrorDocument 400 /link1/error.php
ErrorDocument 401 /link1/error.php
ErrorDocument 403 /link1/error.php?status=403
ErrorDocument 404 /link1/error.php?status=404
ErrorDocument 500 /link1/error.php?status=500
This config works for me 这个配置适合我
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain\.com
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=permanent,L]
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
It is not working for sub directories because those sub-directories have their own rewrite rules and by default these rules are not inherited by lower level .htaccess from their parents. 它不适用于子目录,因为这些子目录有自己的重写规则,默认情况下,这些规则不会被父级的低级别.htaccess继承。
To overcome this you have 2 options: 要解决这个问题,您有两种选择:
www
forcing rules in your lower level .htaccess as well OR www
强制规则以及OR Add this line on top of your lower level .htaccess files: 在较低级别.htaccess文件的顶部添加此行:
RewriteEngine On RewriteOptions Inherit # remaining rules will be below this
UPDATE: Keep your www rule like this: 更新:保持您的www规则如下:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,NE,R=301]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.