htaccess: different rewrite rules for different IP addresses

Is it possible to apply different rewrite rules for different IP addresses using only one .htaccess file?

I have these rules:

RewriteEngine on #RewriteCond %{REMOTE_ADDR} !^123\.456\.789\.123 RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d #RewriteRule ^(.*)$ /version/1.0/index.php?r=$1 [L] RewriteRule ^(.*)$ /version/2.0/index.php?r=$1 [L] 

Both of the last two rules work fine, and I can choose which one to enable. I want to somehow include one rule for myself to work on the development version, and another rule for production.

Is there any way to use IP filtering for this? Is it even possible? Is there another alternative solution so that I can include both rules: one for development and the other for production?

+6
source share
2 answers

Try:

 RewriteEngine on RewriteCond %{REMOTE_ADDR} !^123\.456\.789\.123 RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ /version/1.0/index.php?r=$1 [L] RewriteCond %{REMOTE_ADDR} ^123\.456\.789\.123 RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ /version/2.0/index.php?r=$1 [L] 

You need to duplicate rewriting conditions. Each set of RewriteCond applies only to the next next RewriteRule . Thus, the first rule will apply to everyone who does not get to the web server from the IP address: 123.456.789.123

+3
source

You can also use this:

 Options All -Indexes RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REMOTE_ADDR} !^123\.456\.789\.123 RewriteRule ^(.*)$ /version/1.0/index.php?r=$1 [L,QSA] RewriteCond %{REMOTE_ADDR} ^123\.456\.789\.123 RewriteRule ^(.*)$ /version/2.0/index.php?r=$1 [L,QSA] 
0
source

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


All Articles