What/How would you do with this rewrite?

I changed domain names. I have 1000+ pages. All the paths in the new domain have change significantly. Currently I just used this blanket rule in the htaccess to rewrite all files to the new domain root. RewriteRule (.*) http://www.newdomain/ [R=301,L]. (is that a good way to do that first of all??). But my question really is… I feel lazy doing this. But to write out a 1000 rewrite rules and painstakingly work out the new paths seems like a lot of room for error and it would take me forever. What would YOU do in this case?

A couple years ago I changed the URL structure on a site from one based on an ID number (mysite.com/index.php?id=12) to one using a search engine friendly slug (mysite.com/slug-with-keywords). It was probably somewhere in the neighborhood of 4,000 pages affected. What I did was create a database table mapping the old ID numbers to the new slug, and I created the table by writing a PHP script to do it. It didn’t take that long to write the script to create the map table. Then I wrote an htaccess rule to check if the page request matched the ID number pattern and if so, I rewrote it to a PHP file which queried the database to retrieve the new URL then issued a 301 redirect.

It did not take that long to do and was an effective solution. It took much less time than it would have taken to write out thousands of htaccess redirects which is probably not a good idea anyway.

Ya from what little I know I see that’s the way to do it. That must be so empowering to just be able to write all that yourself. Unfortunately I am not empowered with that stuff.


First, you’ve learned the lesson about planning a site change (I hope).

Second, what you need is a RewriteMap. Unfortunately, you need access to the httpd.conf to configure that as it can easily bring an entire server down for a simple typo (syntax error).

Third, what cd described is what I’ve been calling “A Poor Man’s RewriteMap,” a PHP script which can read a database or just a flat file (essentially, just a table with two columns) which should only be used for 404 situations. Kudos to cd for working that out! While the read-a-file isn’t a difficult thing for PHP to do, I’d just create an array in the 404.php script to do that rather than read a separate file. Of course, the redirection would entail TWO header(“location:…”) lines, one for a 301 status code and the second for the redirection.



Thanks guys for the info. No I new what was coming. Changing the paths was worth it.

If it is PHP and alot of rewrites, you probably want database not flat file. Reason being PHP will have to parse that flat file on every request whereas it can just query the database which will likely be largely more efficent.

If you can get into a persistent environment then flat files become more interesting as you can do something like hydrate a dictionary and keep it in RAM and take advantage of very fast lookups that way.