views:

49

answers:

4

I am planning to convert my website to a new CMS, but I would like to use mod_rewrite to seamlessly redirect old links to their new locations.

The catch is that my new blog will not have the same article numbers as the old, because I'll import some older blog entries in their first. Thus, my mod_rewrite would need to take a URL like old.php?article=125, do the addition to figure out the new article number (say +200, for this example), and redirect to new.php?i=325.

Can mod_rewrite do the addition on its own, or am I going to need some kind of 'helper' script to do that?

A: 

I believe that's pushing the "voodo magic" that is mod_rewrite. I would either design a helper script to follow this up, or actually migrate at a database level the old entries.

The latter would give you much better stability and control over how the system it laid out. You would have new post id's for all the old entries and everything should fit seamlessly.

Josh K
Ah, but I wish to import a couple hundred old posts starting from id 0, and then the newer set (from a different CMS) starting from where that left off. But as SchlaWiener pointed out, I could start the second batch of imports at 1000, and then the rewrite rules easy, like 137 => 1137.
ewall
@ewall: It's worth it to spend a couple hours massaging the data into the table to not have to depend on something like `mod_rewrite`.
Josh K
+1  A: 

It can not do math. Maybe you want to use a rewrite map.

Sjoerd
+1  A: 

Mod rewrite can do regular expressions. If you can define the value of your new articles to be the same id plus 1000 than you can do:

rewriteRule /article=(\d{1})$ /new.php?i=100$1
rewriteRule /article=(\d{2})$ /new.php?i=10$1
rewriteRule /article=(\d{3})$ /new.php?i=1$1
SchlaWiener
Why the downvote? It looks witty and simple :-?
Álvaro G. Vicario
Because its akward to have three rewrite rules for one purpose. But it get's the job done, right ;) And it was slightly wrong because I forgot a $ at the end of the pattern (I fixed that)
SchlaWiener
I did think of that, and it would probably be possible for me to start the imported posts after 1000. (Plus, it would make me look like a really prolific writer!) +1 for cleverness
ewall
+1  A: 

One option is to completely forget about old links in your .htaccess file. Instead, build a custom 404 Not Found error page and configure Apache to serve it on broken links. That page will be a PHP script and it'll test the $_SERVER['REQUEST_URI'] variable against simple expression:

<?php

if( substr($_SERVER['REQUEST_URI'], 0, 7)=='old.php' ){
    // Old link found!
}

?>

Inside the "old link found" condition, you can do further processing:

  1. Run a regular expression to find an article ID
  2. Do math on it and try to match it against a new ID
  3. Perform a permanent redirection with header()
Álvaro G. Vicario
I like the idea, although there are probably faster/easier ways to do it. I wonder if I can override the default error handlers on my web host? Hmm...
ewall
So far, I've been able to use ErrorDocument in all my hosting accounts. I like this approach because it doesn't add extra overload for valid links, which are normally a vast majority.
Álvaro G. Vicario