Welcome to WebmasterWorld Guest from

Forum Moderators: Ocean10000 & incrediBILL & phranque

Dynamic 301 .htaccess Redirects not Working

What am I doing wrong?

2:35 pm on Oct 24, 2012 (gmt 0)

5+ Year Member


I'm trying to redirect old-style dynamic urls to a set of static urls using .htaccess
Wrote this code but it doesn't seem to be doing anything. Where's the mistake please?

RewriteEngine on
RewriteCond %{QUERY_STRING} ^pageid=325$
RewriteRule ^/site/index.cfm$ http://www.example.com/new-url.htm? [L,R=301]

Old url structure is:
and I want it to be:
9:06 pm on Oct 24, 2012 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month

#1 When you say


do you mean your domain (example.com) or do you mean a subdirectory? A RewriteRule only sees the path: the part after http://www.example.com and before the query, if any. In htaccess, leave off the leading slash in the pattern. Include it in the target when you are rewriting alone. (Your Redirect target is correctly formed, so don't change it.)

#2 a rule that explicitly says

will only pick up requests that either asked for this form in the first place (baaad!) or that have already been rewritten by mod_dir. This normally executes after mod_rewrite, but in htaccess you can't make assumptions so rules should allow for both forms:

RewriteRule ^directory(/(index\.cfm)?)?$ et cetera.

The double-nested ? elements are to avoid a double redirect if you get a request missing the final directory slash. Remember to escape all literal periods in the pattern. Sometimes it doesn't make much difference; sometimes it's crucial.

Are you really making 325+ separate rules, or is that just your example?
11:49 pm on Oct 24, 2012 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

RewriteCond %{QUERY_STRING} ^pageid=325(&|$)
RewriteRule ^folder/(index\.cfm)?$ http://www.example.com/new-url.htm? [R=301,L]

The above corrects several faults.

Do you really need the new URL to end ".htm"? I would aim for extensionless URLs on the new site.

Additionally, if there are a large number of these redirects to perform, you would be better off not having a huge list of redirects in the htaccess file.

Instead you should internally rewrite (that's internally rewrite using a single rule, not externally redirect) all of these requests to a special PHP script that looks up the new URL in an array and uses the PHP HEADER directive to send a 301 redirect to the new URL.

RewriteCond %{QUERY_STRING} ^pageid=([0-9]+)(&|$)
RewriteRule ^folder/(index\.cfm)?$ /special-script.php?id=%1 [R=301,L]

You can use a language other than PHP if you want, this was just an example.

This internal rewrite must unusually appear near the beginning of the htaccess file (after the rules that block access, and before rules that redirect). You MUST also ensure that these requests cannot be redirected by the non-www/www rule in the htaccess file (add a RewriteCond exclusion to that rule) otherwise non-www requests for the old URLs will expose the special-script internal path back out on to the web as a new URL and create a double redirect chain for those requests.

That would be a disaster.
7:35 am on Oct 25, 2012 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

The "special script" should also return a 404 Not Found header and "include" your 404 error page code and content when the requested ID number doesn't exist.
2:27 pm on Oct 25, 2012 (gmt 0)

5+ Year Member

@lucy24, yes, I actually meant a subdirectory
It's not 325 redirects, I'll only need 18 of them :)

@g1smd, genius. It's working now! Thank you.

I cannot say I understood the last paragraph but it does sound like a disaster and I'll have to read something about .htaccess. However, after uploading the .htaccess I tried both
They redirect to http://www.example.com/new-url.htm which I suspect is a good thing :)
4:16 pm on Oct 25, 2012 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

Yes. You must test with both www and non-www requests. Use the Live HTTP Headers extension for Firefox to look in detail at the reponses. You need to make sure there are no chains. Try also adding another junk parameter on the end of the request and make sure that request is also correctly redirected.

With only 18 redirects, doing this in htaccess is fine.

You may want to try out the "rewrite to a script" version so that you are familiar with it for when you need it on a really big site. I'm using it to redirect 80 000 duplicate content and old-style URLs to 1200 new extensionless URLs on one site.

The stuff about correct "404" responses was for when you use a PHP (or other) script not a pure htaccess solution.

Featured Threads

My Threads

Hot Threads This Week

Hot Threads This Month