Setting Up 301 Redirects for Dynamic URLs

Setting Up 301 Redirects for Dynamic URLs

Posted by Alex Juel on November 24th, 2008 | Filed Under:

If you’ve ever had to do 301 redirects for one of your websites, you might have been overwhelmed with all of the different ways to do it. That code is really confusing and can be extremely frustrating to figure out. One of the best resources I’ve found so far to walk you through this is .htaccess, 301 Redirects & SEO, an article over at SEOBook.

Once you do set up 301 redirects a couple times and become a bit more familiar with the .htaccess file, it becomes fairly easy. Today was a different story though and the SEObook article above couldn’t help me. I just spent a large chunk of my morning trying to figure out how to redirect dynamic URL’s to static pages.

Here’s an example;

I wanted to redirect


From what I’ve read, some people seem to have luck doing a regular 301 redirect with dynamic URLs, but for most people it won’t. There are so many different server configurations, that sometimes you just might have to try everything you can think of, which is what I had to do. The .htaccess article over at SEObook does mention how to do this, but for the server I was working on, or site configuration (it was a Drupal site), that method just wouldn’t work, so I had to search all over the place to put different methods together.

This is the format that finally worked for me:

RewriteCond %{QUERY_STRING} ^id=1$
RewriteRule ^$ [R=301,L]

So take your URLs and plug them into the appropriate areas

After the question mark in the URL, there is an ID. This might look different for your URL, but you can change that to whatever your URL looks like. For example, it might say page.php?page_num=68 or something to that effect. Then you just replace id=68 with page_num=68 in the redirect code.

Also, don’t forget to add the question mark to the end of the new URL that you’re trying to redirect to.

So if you are redirecting several dynamic URLs, this is what your .htaccess should look like:

RewriteCond %{QUERY_STRING} ^id=1$
RewriteRule ^page.php$ [R=301,L]

RewriteCond %{QUERY_STRING} ^id=72$
RewriteRule ^page.php$ [R=301,L]

RewriteCond %{QUERY_STRING} ^id=46$
RewriteRule ^page.php$ [R=301,L]

This stuff was fairly confusing for me, so I’ve tried to write up my experience the best I could. I hope it helps out if you ever have to try to figure out how to redirect dynamic URLs for a site of your own.

Update: Anyone who has had to do these redirects for a large site, such as an e-commerce site, knows how tedious it can be to create hundreds of redirects to paste into the .htaccess file. Ted Bradley had this same problem and sent me a message with a link to an Excel spreadsheet he created that can do all the dirty work for your. Check out 301 Redirects from Dynamic to Static Generator on his site.

Additional Variations

I’ve been getting lots of requests for odd variations, so if I figure them out or if you’d like to send in your own fixes, please do.

If you have URL’s with multiple IDs, simply expand the expression

Old URL:
New URL:

RewriteCond %{QUERY_STRING} ^id=4&mscsid=49$
RewriteRule ^page.asp$ [R=301,L]

Redirecting many URLs in a single statement (not tested. See comment for more info)

Old URL:
New URL:

RedirectMatch 301 ^page.php?id=(.*).htm$$1.html

Redirecting a page without an ID

Old URL:
New URL:

RewriteCond %{QUERY_STRING} ^/page/55c9/$
RewriteRule ^$ [R=301,L]

Additional Notes

Here are a few things I’ve learned while working with our site visitors and the different types of redirects I get.

  • If your URL has a hash sign, it will not redirect (learn more)

If this post didn’t help answer your question, consider asking it over at . Those guys have always answered all of my crazy redirect questions.


Alex’s specialty at Inflow is to link it up, wrap it up, get it out there and get results. Alex Juel has experience with clients large and small in a wide variety of industries.

53 Comments on “Setting Up 301 Redirects for Dynamic URLs

    Great post! I’ve been trying to figure out how to redirect these individual dynamic URLs for a while now!!! Thank God for your post…helped out a bunch. Keep up the good work.

    Any idea on how to redirect dynamic pages with multiple IDs? e.g.

      @SEOinHawaii thanks for the kind words! To answer your question, I think you’d just have to expand the expression.

      For example, it would have to be written as;

      RewriteCond %{QUERY_STRING} ^id=4&mscsid=49$

      You’d just have to include everything after the question mark. I can’t think of any other way to do more than one ID.

      Hope this helps!

    I´ve been trying to apply 301 redirect on my .htaccess, but It doesn´t seem to work.
    I need to reedirect:

    Heres the code in my .htaccess:

    RewriteCond %{QUERY_STRING} ^id=1$
    RewriteRule ^masajes-masajista-drenaje-linfatico-enfermeria-primeros-auxilios-tecnico-electrocardiograma-reflexologia.php$ [R=301,L]

    what am I doing wrong?

    Thanks in advance!

      It looks like that should work, and clicking your link, it seems it’s redirecting fine. Did you change anything to make it work?

    HI there,

    Great help here and I could help a friend out with his redirect thanks to you!

    Now I have a problem:

    Must redirect from a former framebusting script where blogs has noted dynamic urls like:

    Above dynamic url shall then be redirected in .htaccess to new static url like below:

    Have tried a lot of different redirects but it wont work. Has the page at

    Can someone please help me out here?

    Think I can pay some for this help too, if it works fine!

    Best Regards
    Eric A.

      Hi Eric,

      The redirect examples above should work for what you’re trying to do. It should redirect a dynamic url to a static url.

      Sorry I couldn’t be of more help. Please leave another comment if you figure it out.

    I’ve been trying to figure this out for a couple of hours today! I am sooooo glad i found your post and instructions. They saved my ass! Much appreciation from South Beach!

      Awesome, glad it worked Mike in Miami!

    Great post that got me most of the way.

    But I need to redirect: to

    How would i do this?

    Thank you, exactly what I needed, just 301 redirrected 200 urls with this method, worked like a charm..

    Thank you man… I spent hours on this before finding you…!

    Alex, could you help me with this. I am trying to redirect

    How can i do that?


    I moved the directory structure of my blog, and now all the Google indexed links don’t work. So, what if I want to redirect:


    And also, what if I want every single instance of “” redirected to “” to accommodate for all the dynamic posts?

      Hey Jim

      I think this should work:

      RewriteCond %{QUERY_STRING} ^p=1$
      RewriteRule ^(.*)$ [R=301,L]

      I don’t know how to accomodate for all dynamic posts other than repeating this for each page in your .htaccess

    Please help.
    How do i permanent mod rewrite

    I have several pages like this. How can i do it with one htaccess code?

      Hey Rony,

      This is similar to Jim’s question above. This should work. I’m not sure on how to do this for all dynamic url’s in one .htaccess code without repeating the process.

      RewriteCond %{QUERY_STRING} ^id=17641397$
      RewriteRule ^/news/article$ [R=301,L]

    I would appreciate if someone could help me. I’m getting so confused. I’m not the best coder around.

    I want to redirect (any dynamic page)


    Thank you

    It´s not easy directing dynamic pages.
    I´ve searched all over for the solution that worked for me!
    You made it happen :-)

    Thank you!

    Hello Alex…
    Useful post, but i cant around how to accomplish the following redirect:



    Can you help?

    Thanks a lot

    Please help,

    I’m trying to figure out how to 301 redirect from


    and also redirect second type of url from


    Thanks in advance.

      Hi Greg, Without fooling around with the htaccess file myself, it’s hard for me to give advice on those two examples. These are much more advanced redirect questions, that’s for sure. I can’t answer them myself, but I would definitely recommend you post your question at . I’m sure jdMorgan will be able to figure it out.

    Sounds good, thank you!

    Hmm, I’ve been trying but nothing seems to work. Attempting to redirect the first of these to the second:

    The code I used is:

    RewriteEngine On
    Options +FollowSymLinks
    RewriteCond %{QUERY_STRING} ^name=News&file=article&sid=2$
    RewriteRule ^modules.php$ [R=301,L]

    Not sure if this code that comes before for the page to do google-friendly rewrites is affecting it:

    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]

    Very frustrating.

    Hi extramsg, I’m not sure why it’s not working, but you should try removing RewriteEngine On from the dynamic redirect part. That only needs to be in the htaccess file once and needs to be before the redirects.

    Thanks so much. I moved my site from an old .cfm site to wordpress. There were/are hundreds of .cfm redirects.


    Site used to be a .htm and now it is a WordPress site…….

    Need to redirect an /index.htm to the root of the WordPress site……

    When I add a… redirect 301 /index.htm
    to the htaccess, a looping problem occurs and the site will not load. Any thoughts on how to do with htaccess?

    Thank you,

      Hi Oliver,

      Yes, redirecting index.htm and similar pages doesn’t work the same way. You will need to add this to your htaccess:

      RewriteCond %{THE_REQUEST} ^[A-Z]{3,9} /.*index.htm HTTP/
      RewriteRule ^(.*)index.htm$$1 [R=301,L]


    Thanks for the reply. I found a WP plugin called – Redirection

    It manages 301 redirects and monitors 404 errors. This worked for the /index.htm redirection.

    Can I have your opinion on which method you believe is preferable?

    htacces or WP plugin?


    Redirection is a great one but I like to use the .htaccess mainly because I worry about my site being bogged down by plugins. I also prefer to know what’s necessary to add to the htaccess in case I’m working on a site that isn’t running on WordPress.

    I believe your opinion is very sound advice!

    One more question, should the redirection code you provided go before or after the WP mod rewrite code in the htaccess?

    Thank you again for answering these questions.

      Hi Oliver, It can go after the WP mod rewrite code. I don’t believe it matters too much actually, as long as it comes after “RewriteEngine On”


    i have redirected from this dynamic url ( to static url ( using the following
    RewriteCond %{QUERY_STRING} ^id=4362$
    RewriteRule ^play.php$ [R=301,L]……

    Now my question is i want to redirect all the dynamic url’s like there are as many as 5000 dynamic url’s are there. How to do this in a single statement……….For my site there are almost all the urls have been bookmarked by the users and many of them have indexed in the search engines….

    If i redirect all the dynamic urls with the above method then it will take around 500 statements to accomplish this…i think its a bit lengthy process…..

    Alex please give some good example……

    Awaiting for your response….

      Hi Samiullah,

      You will need a different type of rule to create your redirects if you don’t want to input one rule for each one.

      You will need to use regular expressions for this. This means that all of your URL’s will need to contain the same pattern. Or you can create a new rule for each pattern.

      It would probably look something like this:

      RedirectMatch 301 ^play.php?id=(.*).htm$$1.html

      The (.*) is a variable. The 1.html is what the rule will append to the redirect, so play.php?id=1753 will redirect to 1753.html. This one might work or you might have to play around with it a bit.

      In your example you had asked that it redirect to a “Game_Name.” If you absolutely must have this, the .htaccess file probably isn’t the place you will be setting this stuff up because the regex above needs a pattern and “Game_Name” is random. Instead you will have to set up some php code and a lookup table for your database.

      I’m not familiar with this so I can only lead you to some resources. Here’s some more info on htaccess code that could help:

      If you still can’t figure it out, I would recommend asking the forums at

      And if you decide to do one redirect at a time, you should place the most important redirects at the top of the htaccess so that they are accessed first, reducing slow down from the server trying to find the correct redirect.

    Hi Alex,

    Thanks for your immediate response…Great one…I have one more doubt…i want to redirect to home page if a user tries to access a page like which shows some ugly page…so i want to redirect to home if any user uses this kind of urls..i have as many as 5000 urls for my site…

      Hi Samiullah,

      I think you might have to do the same thing as in my previous comment. Maybe something like this:

      RedirectMatch 301 ^play.php?id=(.*).htm$$

    Hi Alex,
    RewriteCond %{QUERY_STRING} ^id=14$
    RewriteRule ^menu.php$,L

    I get redirect:,L%5d

    But it should be:

    Any idea?

    Hi Itay,

    I think it’s because you don’t have a space after the question mark, so instead it’s appending [R=301,L] to the end of the URL.

    ALEX …. GOD BLESS YOU!!!!!!

    Hello buddy!!
    I am new in .htacess. I have tried so many times to redirect but i couldnot successed. Please can you help me to redirect this url{dynamic ID)
    to{dynamic ID).
    Please guide me!!

    Its me again can you guide me how to get in hyperlink also that link. Like if i have < a href="index.php?option=content&link_id={dynamic ID}" rel="nofollow">Content
    It should be seen in status bar as: index.html/content/?id={dynamic ID}.
    Thank you hope you will guide me!!

    Hi Jay,

    For your first question, this should work:

    RewriteCond %{QUERY_STRING} ^option=content&link_id={dynamic ID)$
    RewriteRule ^index.php${dynamic ID)? [R=301,L]

    For your second question, I’ve never done something like that. Maybe this page can help you;

    You could always just set up redirect so that the clean URL redirects to the long URL instead too.

    oh thanks for your cooperation Mr. Alex.

    Hi Alex,
    Thanks for the tutorial, but I’ve added code to my existing htaccess file to test 1 redirect of:


    with the following code:

    RewriteEngine On
    RewriteCond %{QUERY_STRING} ^p=leviathon$
    RewriteRule ^/vessels.php$ [L,R=301]

    but for some reason it’s NOT working??

    Any ideas???


    Hy to all,

    Here my question:

    I have to rewrite some urls with path like:


    to my new homepage

    I used this:

    RewriteCond %{QUERY_STRING} ^Action=tlc$
    RewriteRule ^main/default.aspx$ [R=301,L]

    the problem is that the final url become:

    but I need only:

    Onother problem is: how I can get a simple rule for multiple page with same pattern “/main/defautl.aspx?Action=”

    Thanks Bye!


    i want to redirect from:

    to another site. But it dont work. Could somebody help me?

    Something like that dont work:
    RewriteEngine On
    RewriteCond %{QUERY_STRING} ^p=760$
    RewriteRule ^(.*)$ [R=301,L]

    My Old Urls :

    my new url there is not.
    I want all oyna.php urls redirect

    My Code is
    RewriteRule ^oyna.php?oyun_id=(.*)$ [L,R=301]

    But not working this code.

    thanks for the great article.

    I have made a redirection following your guideline and it works,
    BUT it adds %3f in the end of the new url.

    Here is my code:

    RewriteCond %{QUERY_STRING} ^main_page=product_info&products_id=301$
    RewriteRule ^index.php$
    /shop/index.php?route=product/product&product_id=301? [R=301,L]

    I think it’s because I still have a ? in my new url and it doesn’t understand what the second one is for…

    Thanks for your help!

      You’re probably right, because %3F is the equivalent of a question mark. A question mark needs to be encoded in a URL for it to work properly.

      Here’s a quick explanation of the “why”.

      So in your case, I’m not completely positive how you can fix it, but maybe you can try replacing it in the .htaccess with %3F. If that doesn’t work, what about adding the html code of a question mark in it’s place, similar to the ampersand in your URL, ?

      I’ve also heard that making the final URL absolute could help fix the problem so you could try adding the etc. to the final URL.

      RewriteCond %{QUERY_STRING} ^main_page=product_info&products_id=301$
      RewriteRule ^index.php$ [R=301,L]

      Here’s a discussion from Webmaster World talking about a similar issue you’re having, which might help you out.

      I hope some of this helps. I don’t have experience with this issue, so if you figure it out, I’d love to hear what you did.

    Hi Alex
    Pls help
    I’m trying redirect this url to

    Ive tried:

    1)RewriteRule ^new.html ?id=11 [NC]

    2)RewriteCond %{QUERY_STRING} ^id=11$
    RewriteRule ^(.*)$ mynew.html.html[R=permanent,L]

    1) allows me to acces the same web page via both urls. I was hoping to be redirected to when I type in the browser.
    2) Send me a 404 error
    Thanks in advance


    Hi Jo,

    Looks like you didn’t even read my post, because neither of the methods you tried are what I wrote about.


    RewriteCond %{QUERY_STRING} ^id=11$
    RewriteRule ^$ [R=301,L]

Comments are closed.