Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

A change in how a site is structured means urls need changing from

site/11-foo/21-bar 

to

site/1-foo/1-bar

i.e. I want to use a RewriteRule to subtract a constant (in this example, 10) from the integer in the 2nd argument and another constant (in this example, 20) from the integer in the 3rd argument.

These simple calculations would save literally millions of 301 redirects that would need handling. Is there any way to do it using mod_rewrite?

share|improve this question
add comment

2 Answers

up vote 4 down vote accepted

No, but you can use a RewriteMap and have your variables processed by an external program:

http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewritemap

MapType: prg, MapSource: Unix filesystem path to valid regular file

Here the source is a program, not a map file. To create it you can use a language of your choice, but the result has to be an executable program (either object-code or a script with the magic cookie trick '#!/path/to/interpreter' as the first line).

This program is started once, when the Apache server is started, and then communicates with the rewriting engine via its stdin and stdout file-handles. For each map-function lookup it will receive the key to lookup as a newline-terminated string on stdin. It then has to give back the looked-up value as a newline-terminated string on stdout or the four-character string ``NULL'' if it fails (i.e., there is no corresponding value for the given key). A trivial program which will implement a 1:1 map (i.e., key == value) could be:

#!/usr/bin/perl
$| = 1;
while (<STDIN>) {
    # ...put here any transformations or lookups...
    print $_;
}
share|improve this answer
    
Excellent. Thanks very much! –  lazysoundsystem Aug 4 '10 at 19:48
    
But take care, this will gain some delay and you can only specify RewriteMap in the context of the server config and virtual host. –  DrDol Aug 14 '10 at 23:13
add comment

There is no calculation within it, http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html

While it does have grouping (count of ocurrences) and mapping i would recommend you to use a global rediction to a main index.php and from there create the page it should be displaying by the request data (get,post,cookie,etc) which is what the big forums and most CMS do (examples are, vbulletin, wordpress, ipb) ...

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.