is it possible to use mathematical operators in .htaccess file? for example i want to redirect a page with id=100 to a page with id=30 ?
Asked
Active
Viewed 1,784 times
3
-
What part is a mathematical expression? Mapping a query string where id=100 to a query string with id=30 as @bradym answered is simple pattern matching and substitution. A mathematical expression would be mapping a query string where id=X to a query string with id=(X-70) for all X in the range of 100 to 1000. – Hitesh Oct 16 '10 at 22:09
-
no,id will have different values,between 1 to n .that is why i said mathematical expression. – hd. Oct 17 '10 at 04:34
-
Can you post an example of your use of RewriteMap? – Alien Life Form Apr 29 '16 at 01:17
1 Answers
3
Assuming you're talking about query strings, yes.
To redirect http://example.com/page.php?id=100
to http://example.com/page.php?id=30
you would do this:
RewriteEngine On
RewriteCond %{QUERY_STRING} id=100
RewriteRule page.php page.php?id=30 [R=301,L]
Edit: AFAIK, it's not possible to do calculations in .htaccess. Send the request to a PHP script where you do the calculation then use the header function to do the redirect (not sure if you're using PHP, but the same principle applies to other languages).
in .htaccess:
RewriteEngine On
RewriteCond %{QUERY_STRING} id=([0-9]*)
RewriteRule page.php calc.php?id=%1 [L]
And in calc.php:
<?php
$base_url = 'http://example.com/destination.php?id=';
if($_GET['id'] < 100){
$new_id = 30;
}
elseif($_GET['id'] >= 100){
$new_id = 40;
}
$url = $base_url.$new_id;
header("Location: $url");
exit();

bradym
- 4,880
- 1
- 31
- 36