number base conversion in .htaccess

允我心安 提交于 2019-12-23 12:33:51

问题


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


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/6732038/number-base-conversion-in-htaccess

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!