htaccess通过index.php路由而不允许www

时间:2013-01-11 00:51:57

标签: php .htaccess url-routing

我通过GoDaddy拥有一个共享主机帐户,我在其上的网站使用.htaccess通过index.php路由所有请求。这样就可以了。

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php [L,QSA]

然后我想限制它使用非www网址,所以我在它之前添加了以下内容:

RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [L,R=301]

总而言之:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php [L,QSA]

但是,这两个一起不起作用。该网址会尝试重定向到/missing.html以查找应路由的网址。另外这两个都很好。有没有办法让两件事情一起工作?

1 个答案:

答案 0 :(得分:0)

您应删除第一个L代码如果您想让它们协同工作。因为L表示如果规则匹配,则不会处理其他规则

RewriteEngine On

RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301]   //remove L from this line

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php [L,QSA]