2016-12-09 45 views
0

我有一个重写映射内部数百个URL重定向/重写的列表。大多数URL包含匹配重写映射中特定条目的查询字符串。我发现this question关于如何将查询字符串传递到重写映射。我得到这个工作正常,但现在的问题是,现有的查询字符串被追加到重写的URL的末尾。例如:通过重写映射后,从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。

回答

0

在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] 

(新添加的重写规则实际上使用这个功能)