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

So take your URLs and plug them into the appropriate areas

Dynamic 301 redirects

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

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