Math statements in htaccess file

Can I use math operators in a .htaccess file? For example, I want to redirect a page with id = 100 to a page with id = 30?

+3
source share
1 answer

Assuming you are talking about query strings, yes.

To redirect http://example.com/page.php?id=100to http://example.com/page.php?id=30, you will do the following:

RewriteEngine On
RewriteCond %{QUERY_STRING} id=100
RewriteRule page.php page.php?id=30 [R=301,L]

Edit: AFAIK, it is not possible to perform calculations in .htaccess. Submit a request to a PHP script where you will perform the calculation, and then use the header function to perform the redirection (not sure if PHP is reused, 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();
+2
source

Source: https://habr.com/ru/post/1769851/


All Articles