Forum Moderators: phranque
This question comes up at least once every week, and there already was a recent post with all the steps, sometime in the last 2 or 3 days.
- changing the links on the page to use the new URLs that users 'see' and 'use'.
List the redirects first, the last redirect being the general non-www to www canonical redirect.
List the rewrites after all of the redirects.
Every rule needs the [L] flag. Use RewriteRule for all of the rules. Do not mix Redirect or RedirectMatch rules in here.
Let's see your example code and we'll offer ways to improve and optimise it.
RewriteCond %{QUERY_STRING} ^SearchResult.aspx?CategoryID=\2$
RewriteRule ^/parent-category/ [NC,R=301,L]
If the scripted approach isn't feasible, then you'll have to have one two-line rewriterule for every old URL on your site that you wish to preserve. In this case the code devolves to that posted many times here -- just a RewriteRule examining the URL-path and a RewriteCond examining the %{QUERY_STRING} variable.
RewriteCond %{query_STRING} ^q=123$
RewriteRule ^index\.php$ /abcd/ [R=301,L]
RewriteCond %{QUERY_STRING} &?id=30&?
RewriteCond %{QUERY_STRING} ^CategoryID=2$ (# would change as I go)
RewriteRule ^SearchResult\.aspx$ /parent-category/? [R=301,L]
RewriteCond %{QUERY_STRING} ^([^&]*&)*CategoryID=2(&.*)?$
RewriteRule ^SearchResult\.aspx$ /parent-category/? [R=301,L]
If the query string has any additional parameters, those rules would fail. I always allow for additional parameters by using 'soft' anchors.
RewriteCond %{QUERY_STRING} &?id=30&?
This changes things from query string must be 'exactly id=30' to query string 'contains id=30'.
With each redirect, do also include both protocol and domain name, not just the path part of the URL in the redirect target.
Note that the query string is delimited by a question mark. It does not *contain* a question mark. You should also include the name of the query parameter, and use a question mark on the end of the rewriterule substitution URL to clear the query string. Therefore, your first example in your most recent post should be:
RewriteCond %{QUERY_STRING} ^CategoryID=2$ (# would change as I go)
RewriteRule ^SearchResult\.aspx$ /parent-category/? [R=301,L]
The query string does not have additional parameters, only queries are the base number (?=thenumber) in both of those urls samples, so doing an exact match looks preferable.