.htaccess没有重定向主页

时间:2015-01-17 13:15:30

标签: php apache .htaccess redirect

这是我在Linux系统上的.htaccess:

ErrorDocument 401 ./error/
ErrorDocument 403 ./error/

RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$

RewriteRule ^(.*)$ http://www.website.co.uk/$1/ [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?page=$1 [QSA]

RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.website.co.uk/$1 [R=301,L]

一切正常; www被添加到非www请求中,并添加了一个尾部斜杠。但是,当访问www.website.com(在cPanel上作为托管域添加)时,用户不会被重定向到www.website.co.uk

如果访问website.com(请注意没有www),那么他们会被重定向。

我需要在.htaccess中添加/更改什么内容?

1 个答案:

答案 0 :(得分:1)

你的规则如下:

ErrorDocument 401 ./error/
ErrorDocument 403 ./error/

RewriteEngine on
RewriteBase /

# if not www.website.co.uk then redirect to it
RewriteCond %{HTTP_HOST} !^www\.website\.co\.uk$ [NC]
RewriteRule ^(.*)$ http://www.website.co.uk/$1 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.*)$ http://www.website.co.uk/$1/ [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?page=$1 [L,QSA]