Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

I wish to set up some 301 redirects to new URLs and I am trying to do this with the redirect facility in cPanel. For some reason the requests are not being processed correctly and I am guessing that this is because the old URLs are query based.

It looks like I will have to modify my .htaccess file manually and my question is: what is the correct syntax to use, based on the examples given below.

http://www.mydomain.com/products.asp?category=42 needs redirecting to http://www.mydomain.com/products/new-category.

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

cPanel will also not be able to determine whether you have other conflicting directives in your .htaccess file.

The RewriteRule pattern matches against the URL-path part of the URL so you will need to use a RewriteCond directive with the %{QUERY_STRING} variable.

RewriteCond %{QUERY_STRING} =category=42
RewriteRule ^products\.asp /products/new-category? [R=301,L]

The = at the start of the RewriteCond pattern essentially performs an exact (string) match (if that is required).

The ? on the end of the RewriteRule substitution removes the querystring from the request, otherwise it will be passed through unchanged.

share|improve this answer
    
RewriteCond %{QUERY_STRING} ^category=5$ [NC] RewriteRule ^products\.asp$ /products/new-category? [R=301,NE,NC,L] –  MrPaulDriver Nov 14 '13 at 9:56
    
I came across a useful tool (link below) which produces slightly different output, as in the comment above. Both solution seem to work. Is first suggestion above the better option? seo-website-designer.com/… –  MrPaulDriver Nov 14 '13 at 10:08
    
"Is first suggestion above the better option?" Yes. ;) To be honest there's not much in it, they both do the same thing. With mod_rewrite and particularly regex there can be several ways to achieve the same goal. However... ^category=5$ is a regex, as opposed to a simple string, as in my example. You don't appear to need a regex and are in fact doing an exact match so I would use the = (exact match) operator. NC (nocase) makes the comparison case-insensitive. Unless you specifically need this to be case-insensitive then don't use it. NE (noescape) is also unnecessary in this instance. –  w3d Nov 15 '13 at 3:56
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.