4

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?

lazysoundsystem
  • 195
  • 1
  • 6

2 Answers2

4

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 $_;
}
Massimo
  • 70,200
  • 57
  • 200
  • 323
  • 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
1

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) ...

Prix
  • 4,881
  • 3
  • 24
  • 25