Can mod_rewrite do the math?

I plan to convert my site to a new CMS, but I would like to use mod_rewrite to smoothly redirect old links to their new locations.

The trick is that my new blog will not have the same article numbers as the old ones, because I will import some old blog entries first. So my mod_rewrite will have to use a url like old.php?article=125 , add an addition to the new article number (like +200 for this example) and redirect to new.php?i=325 .

Can mod_rewrite make the addition on its own, or will I need some kind of “helper” script to do this?

+4
source share
4 answers

He cannot do math. Perhaps you want to use the rewrite card .

+2
source

Reorganization Mod can execute regular expressions. If you can define the value of your new articles as 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 
+3
source

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

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

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

  • Run regex to find article id
  • Do the math and try to match it with the new ID
  • Perform a permanent redirect with the header ()
+2
source

I believe I'm pushing the "voodoo magic" that mod_rewrite . I would either develop a helper script to keep track of this, or actually migrate old records at the database level.

The latter will give you much better stability and control over how the system is laid out. You will have a new message id for all old entries, and everything should be easy to combine.

0
source

Source: https://habr.com/ru/post/1313094/


All Articles