htaccess重定向不在co.uk域上工作

时间:2015-10-21 19:16:32

标签: apache .htaccess mod-rewrite redirect virtualhost

这是我的apache配置:

DocumentRoot "/www/public"

<Directory "/www/public">
    Options Indexes FollowSymLinks
    AllowOverride all
    Order allow,deny
    Allow from all
</Directory>

NameVirtualHost *:80

<VirtualHost *:80>
        ServerAlias www.domain.com domain.com
        DocumentRoot /www/public
        ServerName www.domain.com
</VirtualHost>

<VirtualHost *:80>
        ServerAlias www.domain.co.uk domain.co.uk
        DocumentRoot /www/public
        ServerName www.domain.co.uk
</VirtualHost>

这是我的htaccess的样子:

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
    RewriteCond %{HTTPS}s ^on(s)|
    RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    RewriteCond %{REQUEST_URI} !=/server-status
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^ /index.php [NC,L,QSA]
</IfModule>

如果我访问www.domain.comdomain.comwww.domain.co.uk,一切正常。但是,当我访问domain.co.uk时,它无法正确重定向到www.domain.co.uk。好像co.uk请求似乎没有读取我的htaccess。

2 个答案:

答案 0 :(得分:0)

这是由于www添加规则中使用的正则表达式。将其更改为:

RewriteCond %{HTTP_HOST} ^[^.]+\.(?:[^.]+|co\.uk)$ [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

确保在测试此更改之前清除浏览器缓存。

答案 1 :(得分:0)

解决方案是改变HTACCESS:

RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$

RewriteCond %{HTTP_HOST} (^[^.]+\.[^.]+$|^[^.]+\.co\.uk$)