.htaccess 301 rewrites with question marks

时间:2015-06-30 13:36:55

标签: .htaccess url redirect url-rewriting

I'm using something similar to the following:

RewriteEngine on

Redirect 301 / http://newdomain.co.uk/link/
Redirect 301 /showcase.asp?showcaseid=1 http://newdomain.co.uk/track1
Redirect 301 /showcase.asp?showcaseid=2 http://newdomain.co.uk/track2
Redirect 301 /showcase.asp?showcaseid=3 http://newdomain.co.uk/track3
Redirect 301 /showcase http://newdomain.co.uk/link/tracks

With that in mind any URL other than the mentione would go to http://newdomain.co.uk/link

Which is fine however any of the other URL's that use a "?" go to say http://newdomain.co.uk/link/showcaseid=1 for /showcase.asp?showcaseid=1

Also with the

Redirect 301 /showcase http://newdomain.co.uk/link/tracks

can I just write as follows:

Redirect 301 /showcase tracks

1 个答案:

答案 0 :(得分:0)

You can't match against the query string (everything after the ?) in a Redirect directive. You have to use mod_rewrite's %{QUERY_STRING} variable:

RewriteEngine On

RewriteCond %{QUERY_STRING} ^showcaseid=([0-9])
RewriteRule ^showcase\.asp$ http://newdomain.co.uk/track%1 [L,R=301]

RewriteRule ^showcase$ http://newdomain.co.uk/link/tracks [L,R=301]
RewriteRUle ^$ http://newdomain.co.uk/link/ [L,R=301]

Note that the order is important. You generally want the more general matching rules to be at the end.