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.

1 Answer 1


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.

  • RewriteCond %{QUERY_STRING} ^category=5$ [NC] RewriteRule ^products\.asp$ /products/new-category? [R=301,NE,NC,L] Commented Nov 14, 2013 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/… Commented Nov 14, 2013 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.
    – MrWhite
    Commented Nov 15, 2013 at 3:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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