Your Strategic SEO outsourcing partner

Call us at: 303.905.1504

Navigation

Sections navigation

seOverflow Blog

Setting Up 301 Redirects for Dynamic URLs

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.

Connect with Alex Juel

Alex Juel on Linked In Alex Juel on Linked In Alex Juel on Twitter

29 Responses to “Setting Up 301 Redirects for Dynamic URLs”

  1. SEOinHawaii says:

    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.

  2. SEOinHawaii says:

    Any idea on how to redirect dynamic pages with multiple IDs? e.g. www.website.com/page.asp?id=4&mscsid=49

  3. alex says:

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

  4. karim says:

    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

    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

  5. alex says:

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

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

  7. alex says:

    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.

  8. 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!

  9. alex says:

    Awesome, glad it worked Mike in Miami!

  10. [...] I had to figure out another .htaccess problem the other day, which was about as confusing as my last problem with 301 redirects on dynamic URLs. [...]

  11. douglas mcclurg says:

    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?

  12. alex says:

    Hey Douglas,

    I believe it should look like this;

    RewriteCond %{QUERY_STRING} ^menuid=080$
    RewriteRule ^index\.asp$ http://www.mydomain.com/?page_id=2 [R=301,L]

  13. Henrik says:

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

  14. sophie says:

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

  15. rony says:

    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

  16. Alex Juel says:

    Hi Rony,

    I believe this should work:

    RewriteCond %{QUERY_STRING} ^parent_id=43$
    RewriteRule ^resource/linksdir/natural-cure-links\.php$ http://www.buydominica.com/resource/Caribbean-43.html [R=301,L]

  17. Jim says:

    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:

    www.whatever.com/?p=221

    to

    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?

  18. rony says:

    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?

  19. Alex Juel says:

    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

  20. Alex Juel says:

    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]

  21. Ron says:

    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

  22. Eigil says:

    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!

  23. Shafiq says:

    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

  24. Greg says:

    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.

  25. Alex Juel says:

    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.

  26. Greg says:

    Sounds good, thank you!

  27. extramsg says:

    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.

  28. Alex Juel says:

    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.

  29. Paul says:

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

Leave a Reply

Stay Informed:

  • AddThis Social Bookmark Button
  • AddThis Feed Button