【问题标题】:Remove Query String from the end of the URL after passing through Rewrite Map通过 Rewrite Map 后从 URL 的末尾删除 Query String
【发布时间】:2017-04-25 01:13:06
【问题描述】:

我在重写映射中有数百个 URL 重定向/重写的列表。大多数 URL 包含与重写映射中的特定条目匹配的查询字符串。我找到了this question,了解如何将查询字符串传递到重写映射中。我得到了这个工作正常,但现在的问题是现有的查询字符串被附加到重写 URL 的末尾。例如:

Expected Rewrite:
/subdir/dir.cfm?categoryID=123 -> https://example.com/subdir/endurl   

Actual Rewrite:
https://example.com/subdir/endurl?categoryID=123

Expected Rewrite
/subdir/dir.cfm?videoID=3422424-131FDDFD-234 -> https://example.com/subdir/awesome/stuff

Actual Rewrite:
https://example.com/subdir/awesome/stuff?videoID=3422424-131FDDFD-234

这是我的重写规则:

RewriteEngine on

RewriteMap redirects dbm=sdbm:C:\Apache24\conf\redirects.sdbm
RewriteCond ${redirects:$1} !=""
RewriteRule ^(.*\.(cfm|cfml)) ${redirects:$1?%{QUERY_STRING}} [NC,R=301,L]

重写后如何删除附加到 URL 的查询字符串?

编辑

我实际上可以使用这个来完成这个工作:

RewriteMap redirects dbm=sdbm:C:\Apache24\conf\redirects.sdbm
RewriteCond ${redirects:$1} !=""
RewriteRule ^(.*\.(cfm|cfml)) ${redirects:$1?%{QUERY_STRING}} [NC,C]
RewriteRule (.*) $1? [L,R=301]

但是我不确定是否有人知道更好的方法来完成我正在做的事情?我认为如果我必须将包含查询字符串的 URL 重定向到包含查询字符串的另一个 url,这可能会中断。

【问题讨论】:

    标签: apache mod-rewrite url-rewriting httpd.conf rewritemap


    【解决方案1】:

    在 Apache 2.4.0 及更高版本中,您可以使用[QSD] 标志(查询字符串丢弃):

    RewriteRule ^(.*\.(cfm|cfml)) ${redirects:$1?%{QUERY_STRING}} [QSD,NC]
    

    在 Apache 2.2 版本中,没有 [QSD] 标志,但如果替换 URI 包含查询字符串,RewriteRule 的默认行为是丢弃现有的查询字符串,因此只需添加一个 ? 到你重写 URI,你会得到同样的效果:

    RewriteRule ^(.*\.(cfm|cfml)) ${redirects:$1?%{QUERY_STRING}}? [NC]
    

    (你新添加的重写规则实际上使用了这个特性)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-06-14
      • 2023-01-18
      • 2016-04-13
      • 2019-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多