【问题标题】:htaccess routing through index.php without allowing wwwhtaccess 通过 index.php 路由而不允许 www
【发布时间】:2013-01-11 00:51:57
【问题描述】:

我有一个通过 GoDaddy 共享的主机帐户,我拥有的网站使用 .htaccess 通过 index.php 路由所有请求。这本身就可以正常工作。

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

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

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]

但是,将这两者放在一起是行不通的。该 URL 尝试重定向到 /missing.html 以获取应路由的 URL。分开这两个都很好。有没有办法让这两件事一起工作?

【问题讨论】:

    标签: php .htaccess url-routing


    【解决方案1】:

    如果你想让它们一起工作,你应该删除第一个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] 
    

    【讨论】:

    • 不幸的是,这适用于 URL 路由,但如果我输入“www.”在应该路由的 URL 之前,它会将我带到 /missing.html
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-06
    • 2012-04-15
    • 2014-12-31
    • 1970-01-01
    • 2017-06-28
    • 2011-06-17
    • 1970-01-01
    相关资源
    最近更新 更多