【问题标题】:.htaccess url changes when accessing from non www从非 www 访问时 .htaccess url 发生变化
【发布时间】:2016-07-17 02:37:44
【问题描述】:

我的 .htaccess 上有这个

Options -SymLinksIfOwnerMatch +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]

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

据我所知,这会将所有参数传递给 $_GET['url'] 并将我的 url 强制为 www。 但是当我访问网址时

http://www.example.net/user/login

它工作得很好,但是当我删除“www”时。 url变成这样

http://www.example.net/index.php?url=user/login

【问题讨论】:

    标签: php apache .htaccess server xampp


    【解决方案1】:
    # Rewrite|Redirect non-www to www
    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
    RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    
    # Rewrite|Redirect www to non-www
    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
    RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    

    【讨论】:

    • 感谢您的回答,我已经尝试了您的示例并解决了问题,但现在它不再将任何参数传递给 $_GET 变量。
    • 再次修改你的代码来解决这个问题,谢谢。
    • @MashiruAlexis - 您想将 url=user 查询字符串附加到所有 URI 还是只是 index.php 页面/脚本或只是 /login URI?根据您在问题中发布的内容,我认为您希望以这个 URI 和查询字符串结束:http://www.example.net/user/login?url=user 仅用于 /login URI。那是对的吗?如果没有,则发布您想要的 URI 和查询字符串作为最终结果。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-08
    • 1970-01-01
    • 1970-01-01
    • 2014-12-16
    相关资源
    最近更新 更多