【问题标题】:htaccess redirection with querystringhtaccess 使用查询字符串重定向
【发布时间】:2011-06-17 19:18:34
【问题描述】:

如何重定向以下网址 http://test.com/changefile.php?filename=top-ranked-colleges

http://test.com/top-ranked-colleges.php 使用 htaccess 重定向。

请任何人帮助我。提前致谢。

【问题讨论】:

    标签: php .htaccess mod-rewrite


    【解决方案1】:

    试试这个

    RewriteCond %{QUERY_STRING} ^filename=(.*)$
    RewriteRule .* http://test.com/%1.php? [R=301,L]
    

    http://test.com/changefile.php?filename=top-ranked-colleges 更改为http://test.com/top-ranked-colleges.php

    RewriteCond %{REQUEST_URI} ^/(.*).php$
    RewriteRule .* http://test.com/changefile.php?filename=$1 [L]
    

    http://test.com/top-ranked-colleges.php 更改为http://test.com/changefile.php?filename=top-ranked-colleges

    【讨论】:

      【解决方案2】:

      确实工作:

      RewriteCond %{REQUEST_URI}  ^/changefile\.php$
      RewriteCond %{QUERY_STRING} ^filename=([0-9a-z_-]+)$
      RewriteRule ^(.*)$ http://website.com/%1.php? [R=301,L]
      

      说明

      %1 来自:RewriteCond %{QUERY_STRING} ^filename=([0-9a-z_-]+)$

      在 RewriteRule 上有一个? 会阻止将原始查询字符串添加到重定向中

      如果被调用的文件是 changefile.php,并且其中有一个带有 filename= 的查询字符串,则使用 %1 作为要转到的页面进行重定向。

      如果你使用 RewriteBase,你也需要添加这个:

      例如:

      RewriteBase /test/
      
      RewriteCond %{REQUEST_URI}  ^/test/changefile\.php$
      

      不起作用

      RewriteRule ^changefile.php?filename=([a-z0-9-]+)$ http://test.com/$1.php? [R=301,L]
      

      这是因为您无法使用 RewriteRuleRedirectMatch 检查查询字符串

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-03-31
      • 1970-01-01
      • 2021-04-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多