【问题标题】:RewriteCond for url with parameters带有参数的 url 的 RewriteCond
【发布时间】:2011-11-06 05:37:13
【问题描述】:

我在重写 url 时遇到了问题。 我想要这个:

http://www.foo.com/test.php?u=s1&id=12345&img=12

http://app.foo.com/12345-s1-12.test

第一个参数u是字符串,参数id和img是整数。

我已经开始这样做了:

RewriteCond %{REQUEST_URI} ^/test.php?u=(.*)&id=(.*)&img=(.*)/ [NC]
RewriteRule (.*) http://app.foo.com/%2-%1-%3.test [QSA,R=301,L]

谢谢:)

编辑:

还是不行,但我已经接近了!

RewriteCond %{REQUEST_URI} ^/test.php [NC]
RewriteCond %{QUERY_STRING} ^u=(.*)&id=(.*)&img=(.*)
RewriteRule (.*) http://app.foo.com/%2-%1-%3.test [QSA,R=301,L]

现在它给了我那个链接:

http://app.foo.com/12345-s1-12.test?u=s1&id=12345&img=12

而不是:

http://app.foo.com/12345-s1-12.test

:(

【问题讨论】:

    标签: apache mod-rewrite url-rewriting


    【解决方案1】:

    解决了!

    RewriteCond %{REQUEST_URI} ^/test.php [NC]
    RewriteCond %{QUERY_STRING} ^u=(.*)&id=(.*)&img=(.*)
    RewriteRule (.*) http://app.foo.com/%2-%1-%3.test? [R=301,L]
    

    在 RewriteRule 的末尾放置 ? 会删除以前的查询字符串信息。

    【讨论】:

    • 在 Apache 2.4 上,您可以使用 QSD(查询字符串丢弃)标志,而不是将 ?(空查询字符串)附加到 substitution 字符串的末尾.
    【解决方案2】:

    这很完美!我用过这个 - 但我想知道为什么目标网址最后显示“%3f”。

    我正在使用:

       RewriteCond %{REQUEST_URI} ^/ie/xx/yy.php [NC]
       RewriteCond %{QUERY_STRING} ^rubric=(.*)&nr=(.*)
       RewriteRule (.*) https://www.newdom.com/xx/yy.php?rubric=%1&nr=%2 [R=301,L]
    

    为什么最后是 %3f?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-08-14
      • 1970-01-01
      • 2020-04-03
      • 2020-07-21
      • 2017-03-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多