Setting Up 301 Redirects for Dynamic URLs
1-877-660-3735

Blog email subscribe

  • This field is for validation purposes and should be left unchanged.

Setting Up 301 Redirects for Dynamic URLs

Posted by Alex Juel on November 24th, 2008

Filed Under:

Share This Post:

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

http://www.example.com/page.php?id=68

to

http://www.example.com/content/page

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 ^$ http://www.example.com/content/page? [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$ http://www.example.com/content/page? [R=301,L]

RewriteCond %{QUERY_STRING} ^id=72$
RewriteRule ^page.php$ http://www.example.com/content/page2? [R=301,L]

RewriteCond %{QUERY_STRING} ^id=46$
RewriteRule ^page.php$ http://www.example.com/content/page3? [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: http://www.example.com/page.asp?id=4&mscsid=49
New URL: http://www.example.com/page.asp?id=4&mscsid=49

RewriteCond %{QUERY_STRING} ^id=4&mscsid=49$
RewriteRule ^page.asp$ http://www.example.com/content/page1? [R=301,L]


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

Old URL: http://www.example.com/page.php?id=1111
New URL: http://www.example.com/content/page_name

RedirectMatch 301 ^page.php?id=(.*).htm$ http://www.example.com/content/$1.html


Redirecting a page without an ID

Old URL: http://www.example.com/?/page/55c9/
New URL: http://www.example.com/newpage.php/page/55c9/

RewriteCond %{QUERY_STRING} ^/page/55c9/$
RewriteRule ^$ http://www.example.com/newpage.php/page/55c9/? [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 http://www.webmasterworld.com/apache/ . Those guys have always answered all of my crazy redirect questions.

About 

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.

Share This Post:

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. http://www.website.com/page.asp?id=4&mscsid=49


      @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!


    Hi,
    I´ve been trying to apply 301 redirect on my .htaccess, but It doesn´t seem to work.
    I need to reedirect:
    http://www.cursoscentromedico.com.ar/masajes-masajista-drenaje-linfatico-enfermeria-primeros-auxilios-tecnico-electrocardiograma-reflexologia.php?id=1

    to

    http://www.cursos-de-masajes.com.ar
    Heres the code in my .htaccess:

    RewriteCond %{QUERY_STRING} ^id=1$
    RewriteRule ^masajes-masajista-drenaje-linfatico-enfermeria-primeros-auxilios-tecnico-electrocardiograma-reflexologia.php$ http://www.cursos-de-masajes.com.ar? [R=301,L]

    what am I doing wrong?

    Thanks in advance!
    Karim


      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:

    http://www.mysite.se/index.htm?freeinfo.htm

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

    http://www.mysite.se/freeinfo.htm

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

    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:

    http://www.mydomain.com/index.asp?menuid=080 to
    http://www.mydomain.com/?page_id=2

    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

    http://www.buydominica.com/resource/linksdir/natural-cure-links.php?parent_id=43
    to
    http://www.buydominica.com/resource/Caribbean-43.html

    How can i do that?

    thanks


    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:

    http://www.whatever.com/?p=221

    to

    http://www.whatever.com/blog/?p=221

    And also, what if I want every single instance of “www.whatever.com/?p=” redirected to “www.whatever.com/blog/?p=” to accommodate for all the dynamic posts?


      Hey Jim

      I think this should work:

      RewriteCond %{QUERY_STRING} ^p=1$
      RewriteRule ^(.*)$ http://www.example.com/blog/?p=221? [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
    http://www.caribbea.net/news/article?id=17641397
    http://www.caribbea.net/index.pl/article?id=17641397

    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$ http://www.caribbea.net/index.pl/article?id=17641397? [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
    http://www.guldenophthalmics.com/ccp51/cgi-bin/cp-app.cgi?usr= (any dynamic page)

    To: http://www.guldenophthalmics.com/ccp7/ecom-prodidx/COREseo.html

    Thank you
    Ron


    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:

    From:
    http://www.domain.co.uk/training-courses/?id=7

    To:
    http://www.domain.co.uk/electrical-training-courses/inspection-testing-electrical-installations-7

    Can you help?

    Thanks a lot


    Please help,

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

    http://www.domain.com/?rated

    to

    http://www.domain.com/top

    and also redirect second type of url from

    http://www.domain.com/?s=free+files

    to

    http://www.domain.com/sh.php?q=free+files

    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 http://www.webmasterworld.com/apache/ . 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:

    http://www.extramsg.com/modules.php?name=News&file=article&sid=2
    http://extramsg.com/?p=1

    The code I used is:

    RewriteEngine On
    Options +FollowSymLinks
    RewriteCond %{QUERY_STRING} ^name=News&file=article&sid=2$
    RewriteRule ^modules.php$ http://extramsg.com/?p=1 [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.


    Hello,

    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…… http://www.ptoutdoors.com.

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

    Thank you,
    OB


      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$ http://www.yoursite.com/$1 [R=301,L]


    Alex,

    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?

    Oliver


    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.
    Oliver


      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”


    Hello,

    i have redirected from this dynamic url (http://www.example.com/play.php?id=1753) to static url (http://www.example.com/play/Game_Name) using the following
    RewriteCond %{QUERY_STRING} ^id=4362$
    RewriteRule ^play.php$ http://www.example.com/play/Game_Name? [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$ http://www.example.com/play/$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:

      http://www.askapache.com/htaccess/301-redirect-with-mod_rewrite-or-redirectmatch.html

      If you still can’t figure it out, I would recommend asking the forums at http://www.webmasterworld.com/apache/.

      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 http://www.sitename.com/play.php?id=1753 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$ http://www.example.com/$


    Hi Alex,
    RewriteCond %{QUERY_STRING} ^id=14$
    RewriteRule ^menu.php$ http://www.mynewdomain.com/our-guides/our-guides?R=301,L

    I get redirect:
    http://www.mynewdomain.com/our-guides/our-guides?%5bR=301,L%5d

    But it should be:
    http://www.mynewdomain.com/our-guides/our-guides

    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 http://www.example.com/index.php?option=content&link_id={dynamic ID)
    to
    http://www.example.com/index.html/content/?id={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$ http://www.example.com/index.html/content/?id={dynamic ID)? [R=301,L]

    For your second question, I’ve never done something like that. Maybe this page can help you; http://www.openjs.com/articles/ajax/target_url_hiding.php

    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:

    http://www.example.com/vessels.php?p=leviathon

    to

    http://www.example.com/leviathan.php

    with the following code:

    RewriteEngine On
    RewriteCond %{QUERY_STRING} ^p=leviathon$
    RewriteRule ^/vessels.php$ http://www.example.com/leviathan.php? [L,R=301]

    but for some reason it’s NOT working??

    Any ideas???

    Thanks


    Hy to all,

    Here my question:

    I have to rewrite some urls with path like:

    /main/defautl.aspx?Action=hereavariable

    to my new homepage

    I used this:

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

    the problem is that the final url become:

    http://mynewdomain.com/?Action=tlc

    but I need only:

    http://mynewdomain.com
    ______________________________

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

    Thanks Bye!


    Hello,

    i want to redirect from:

    http://www.baby-modern.de/gratisbutler/?p=798

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

    Something like that dont work:
    RewriteEngine On
    RewriteCond %{QUERY_STRING} ^p=760$
    RewriteRule ^(.*)$ http://www.example.com/blog/?p=221? [R=301,L]


    Hi,
    My Old Urls :
    oyunambari.com/oyna.php?oyun_id=2442
    oyunambari.com/oyna.php?oyun_id=2443
    oyunambari.com/oyna.php?oyun_id=2444
    oyunambari.com/oyna.php?oyun_id=3434
    …………….
    oyunambari.com/oyna.php?oyun_id=8991
    oyunambari.com/oyna.php?oyun_id=8992

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

    My Code is
    RewriteRule ^oyna.php?oyun_id=(.*)$ http://www.oyunambari.com [L,R=301]

    But not working this code.
    Help.
    Thanks


    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”.

      http://www.blooberry.com/indexdot/html/topics/urlencoding.htm

      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 http://www.yoursite.com/ etc. to the final URL.

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

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

      http://www.webmasterworld.com/apache/4138119.htm

      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
    http://www.mywdomain.com/?id=11 to http://www.mywdomain.com/new.html

    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 http://www.mywdomain.com/new.html when I type http://www.mywdomain.com/?id=11 in the browser.
    2) Send me a 404 error
    Thanks in advance

    Jo


    Hi Jo,

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

    Try

    RewriteCond %{QUERY_STRING} ^id=11$
    RewriteRule ^$ http://www.mywdomain.com/new.html? [R=301,L]

Comments are closed.