【问题标题】:Rewrite rule with query string用查询字符串重写规则
【发布时间】:2015-09-10 10:33:59
【问题描述】:

在重写规则方面需要一点帮助。 如何将带有查询的 url 重定向到带有查询的另一个 url? 示例:

http://example.com/prject/test.php?key=1

重定向到

http://example.com/prject/test.php?id=1

还有

http://example.com/prject/file2.php?key=1

http://example.com/prject/file2.php?arg=1

.htaccess 文件位于 /project/ 目录下。

【问题讨论】:

    标签: .htaccess mod-rewrite url-rewriting query-string


    【解决方案1】:

    在您的 /project/.htaccess 文件中,插入以下内容:

    RewriteEngine On
    RewriteBase /project/
    
    # Redirect test.php?key=<number> to test.php?id=<number>
    RewriteCond %{QUERY_STRING} key=(\d+)
    RewriteRule ^test.php$ test.php?id=%1 [R,L]
    
    # Redirect file2.php?key=<number> to file2.php?arg=<number>
    RewriteCond %{QUERY_STRING} key=(\d+)
    RewriteRule ^file2.php$ test.php?arg=%1 [R,L]
    

    【讨论】:

      【解决方案2】:

      试试这个:

      RewriteEngine On
      RewriteCond %{QUERY_STRING} ^key=([^&]+) [NC] 
      RewriteRule ^ http://example2.com/prject/test.php?key=%1 [NC,R,L]
      

      【讨论】:

      猜你喜欢
      • 2016-08-13
      • 2016-11-01
      • 2011-04-18
      • 1970-01-01
      • 1970-01-01
      • 2012-03-11
      • 1970-01-01
      • 2016-12-08
      • 2013-03-19
      相关资源
      最近更新 更多