4

I intend to do a url redirection of the form :

from:

domain.com/A

into

domain.com/someResourse/id/10

in this redirection the base of the id is changed from 16 to 10.

I wonder if this is possible using .htaccess

regilero
  • 29,806
  • 6
  • 60
  • 99
olix20
  • 794
  • 1
  • 11
  • 23

2 Answers2

4

You could use RewriteMap with the external program mapping (prg:). This is a quite adavnced use of mod-rewrite and even of RewriteMap.

RewriteLock /var/lock/rewritemaplock.lock
RewriteMap base16to10 prg:/somewher/modrewritemapbase16to10.pl
RewriteRule - ${base16to10:%{REQUEST_URI}}

And for the perl script (or any other language), not tested

#!/usr/bin/perl
$| = 1; # Turn off I/O buffering
while ($uri=<STDIN>) {
    sprintf("/someResourse/id/%d",hex($uri)); 
}

You may need to test and extend the program, at least you need to return the "NULL" string in case of error. You may need also to add some RewriteCond before calling this rewriteRule if some other url shouldn't be converted.

You can use other languages for the base 16 to 10 rewriter, here's an example in PHP.

regilero
  • 29,806
  • 6
  • 60
  • 99
  • Thanks @regilero. but adding either of `RewriteLock /test.lock` or `RewriteMap base16to10 prg:/baseConvert.php` causes an internal server error (error 500). any advise? – olix20 Jul 18 '11 at 15:20
  • well the rewriteLock is a physical path, on disk maybe you cannot write anything on your root filesystem (/), same for your php script, give the full disk path, and check the apache error.log – regilero Jul 18 '11 at 15:58
  • 2
    @Md Oliya, @regilero Both `RewriteLock` and `RewriteMap` CANNOT be declared in .htaccess file (hence the 500 error) -- only in server configuration / virtual host context: http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritelock – LazyOne Jul 18 '11 at 18:19
  • @LazyOne: thanks, of course, so using a `` tag instead of the .htaccess file should do the trick (.htaccess files are bad). – regilero Jul 18 '11 at 23:03
  • @regilero As you can see many people are unaware of this (I'm talking about those who asking questions). So you should really mention this small moment when giving such advices to avoid confusion (the answer is absolutely fine, it just lacks this explanation IMHO). **P.S.** On a side note -- OP was asking if it is possible with .htaccess (only?). – LazyOne Jul 18 '11 at 23:09
  • LazyOne: on a side note as well many people asking for .htaccess are in fact asking for apache configuration without knowing .htaccess has limitations and `` is equivalent without limitations. – regilero Jul 19 '11 at 08:24
1

It's not possible to do calculations in .htaccess. but you could send the request to a PHP (or some other languages) script where you do the calculation then use the header function to do the redirect.

Phil Eddies
  • 208
  • 1
  • 6
  • This is recommended solution. Another approach -- use [RewriteMap](http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritemap) with **MapType: prg** (External Rewriting Program) -- which pretty much the same, but done differently. :) – LazyOne Jul 18 '11 at 11:42