How to 301 Redirect Specific URLs with Question Marks in an .htaccess file
Warning Warning: The following post might get a little nerdy!
The other day I was running into an issue when I was trying to redirect a page with a URL that contained a question mark (?) to a new search engine friendly URL.
Example:
http://www.domain.com/category.apsx?id=1 to http://www.domain.com/keyword-category-1.php
Doing a traditional style line by line redirect in an .htaccess file was not working:
RedirectPermanent /category.apsx?id=1 http://www.domain.com/keyword-category-1.php
Long story short the question mark in the old URL was breaking the 301 redirect and making it useless.
The solution is a Mod Rewrite with a 301 redirect:
After some in depth searching of the internet and testing I figured it out.
Below is the code that you need to use:
RewriteCond %{QUERY_STRING} id=1
RewriteRule category \.aspx http://www.domain.com/keyword-category-1.php [R=301,L]
Hopefully this will help people looking for this very specific 301 redirect issue. If you have feedback or input please comment below!
April 23, 2009 9:09 am SEO












April 24th, 2009 at 8:40 am
If you work in e commerce SEO like I do you are bound to run into this issue updating old product pages to new ones. In the past I have had to bring in the IT team or off shore this type of work. Great post, I will be bookmarking this for the next time I run into this 301 issue.
April 24th, 2009 at 9:26 am
Great post. I am sure this has racked the brains of many and any other professionals who are lucky enough to read this should definitely be able to apply this to their existing clients.
May 14th, 2009 at 10:28 pm
I just loved CJ’s comment, its true must have caused a little stir among those in the know, and for sure that it has helped them out with their clients indeed,
great and informative read.
July 17th, 2009 at 3:34 pm
hello there…
what if the “keyword-category” word changes for every id ?
then how to proceed ?
July 22nd, 2009 at 6:22 am
pushpinder - If your “keyword-category” word changes for every id that is ok because it is the destination url.
If you give me an example I might be able to help out.
September 4th, 2009 at 8:02 am
I am sure this is what i need as well but I can not get it to work…
I have made some changes to our site and need to make some re-directs so the pages are redirected from google so we do not lose positioning from highy natural ranked pages
eg.
I need to get
http://www.christmas-events.com/view.php?title_private=Hac_Shared
to become
http://christmas-events.com/shared-christmas-party/Hac-Shared
I can not do a simple re-direct as I think that the ? i smucking it up - can anyone help?
September 8th, 2009 at 5:58 am
hi,
I am having trouble too…
Trying to get blog pages and posts to redirect to another blog..
For example: I am trying to make this page http://greenesrelease.com/?page_id=3725
go to this page http://greenesrelease.com/grief/?page_id=153
my pages have the code: ?page_id=3725
and my posts have the code: ?p=5140
Any suggestions?