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]

301 redirect
Click to view larger image

Hopefully this will help people looking for this very specific 301 redirect issue. If you have feedback or input please comment below!

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Mixx
  • Reddit
  • StumbleUpon
  • Technorati
9:09 am SEO

7 Responses

  1. Mike Says:

    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.

  2. CJ Says:

    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.

  3. Janet | Web Design Says:

    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.

  4. pushpinder Says:

    hello there…

    what if the “keyword-category” word changes for every id ?

    then how to proceed ?

  5. Stephen Peron (evisibility) Says:

    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.

  6. Matt Keen Says:

    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?

  7. Janet Says:

    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?

Leave a Comment

Your comment

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.