【问题标题】:url rewrite both www to non-www and delete parameter index.php?url when use phpmvc使用 phpmvc 时,url 将 www 重写为非 www 并删除参数 index.php?url
【发布时间】:2020-01-24 16:46:31
【问题描述】:

我想将www.myDomain.com/index.php?url=controller/methode/parameter 重定向到 mayDomain.com/controller/methode/parameter

这是我的 htaccess 代码:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l

RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]

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

问题是当在开头的 url 中使用 www 时,index.php?url= 出现在 url 中! 有什么办法不显示这个吗?

【问题讨论】:

    标签: .htaccess redirect url-rewriting


    【解决方案1】:

    您的指令顺序错误。您的规范重定向需要在您的内部重写之前

    要删除 URL 参数,您需要额外的重定向。但是,这假设您的内部 URL 都使用规范格式,即。 /controller/methode/parameter。 (重定向仅适用于任何反向链接。)

    请尝试以下方法:

    RewriteEngine on
    
    # Remove "url" parameter if requested directly
    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteCond %{QUERY_STRING} ^url=([^&])*
    RewriteRule ^index\.php$ http://example.com/%1 [R=301,L]
    
    # Canonical www to non-www redirect
    RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
    RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
    
    # Front-controller
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-l
    RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
    

    检查REDIRECT_STATUS 环境变量的条件对于防止重写循环是必要的,因为我们只想重定向直接请求而不是已经重写的请求。

    您需要在测试前清除浏览器缓存。

    【讨论】:

      猜你喜欢
      • 2013-10-23
      • 2015-11-06
      • 1970-01-01
      • 2013-10-08
      • 1970-01-01
      • 2014-12-16
      • 1970-01-01
      • 2018-03-31
      • 1970-01-01
      相关资源
      最近更新 更多