htaccess重定向多个查询字符串(无论它们出现的顺序如何)

时间:2018-02-19 02:36:45

标签: .htaccess redirect url-rewriting

所以我有一个拥有旧查询字符串网址的网站。但问题是,这些查询字符串的多个版本已在Google中编入索引并在网站上进行了抓取。它们将被重定向到新的SEF网址。

所以,目前我有这段代码:

RewriteCond %{REQUEST_URI} ^/mls/property.cfm [NC]
RewriteCond %{QUERY_STRING} ^mlsid=(.*)&wsid=(.*)&mlsnumber=(.*)
RewriteRule (.*) /mls-redirects.cfm?mlsid=%1&wsid=%2&mlsnumber=%3 [QSA,L]

mls-redirects.cfm正在进行重定向。因此,当url(并且工作得很好!)具有MLSID,WSID和MLSNUMBER的ORDER中的查询字符串时,这将处理。但是如果WSID是第一个或最后一个,这不起作用。等等。

不是使用所有不同的代码迭代,有没有办法不使这个“查询字符串序列敏感”? HA!

感谢所有帮助!

编辑:也尝试过:

RewriteCond %{REQUEST_URI} ^/mls/property.cfm [NC]  
RewriteCond %{QUERY_STRING} mlsid=(.*)  
RewriteCond %{QUERY_STRING} wsid=(.*)  
RewriteCond %{QUERY_STRING} mlsnumber=(.*)  
RewriteRule (.*) /mls-redirects.cfm?mlsid=%1&wsid=%2&mlsnumber=%3 [R=301,L]  
像这样?这会产生以下网址:/mls-redirects.cfm?mlsid=86278683&wsid=&mlsnumber=

我觉得我们很接近!

1 个答案:

答案 0 :(得分:0)

这样的事情应该有用(或至少帮助你走上正轨):

## Set "a" as an empty environmental variable
RewriteRule ^ - [E=a]
## Set "b" as an empty environmental variable
RewriteRule ^ - [E=b]
## Set "c" as an empty environmental variable
RewriteRule ^ - [E=c]

## If there's a key of "mlsid" in the query string,
## assign its value to "a"
RewriteCond %{QUERY_STRING} mlsid=([^&$]+)
RewriteRule ^ - [E=a:%1]

## If there's a key of "wsid" in the query string,
## assign its value to "b"
RewriteCond %{QUERY_STRING} wsid=([^&$]+)
RewriteRule ^ - [E=b:%1]

## If there's a key of "mlsnumber" in the query string,
## assign its value to "c"
RewriteCond %{QUERY_STRING} mlsnumber=([^&$]+)
RewriteRule ^ - [E=c:%1]

## Final checks
RewriteCond %{REQUEST_URI} ^/mls/property.cfm [NC]

## At least one of our variables needs a non-empty value
## (remove [OR] from each line to require the variables to have a value)
RewriteCond %{ENV:a} !^$ [OR]
RewriteCond %{ENV:b} !^$ [OR]
RewriteCond %{ENV:c} !^$

## You could also delete the above three lines and
## uncomment the next line instead
#RewriteCond %{QUERY_STRING} (mlsid|wsid|mlsnumber)(=)(.*)

RewriteRule (.*) /mls-redirects.cfm?mlsid=%{ENV:a}&wsid=%{ENV:b}&mlsnumber=%{ENV:c} [L,R=302]

希望有所帮助。