Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 enable one rule for myself in order to work on development version, and other rule for production.

Can I use somehow IP address filtering for that? Is that even possible? Is there any other alternative solution so I can have both two rules enabled, one for my development and other one for production?

like image 657
gradosevic Avatar asked Oct 15 '13 20:10

gradosevic


Video Answer


1 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 the rewrite conditions. Each set of RewriteCond's only apply to the immediately following RewriteRule. So the first rule will get applied to everyone who isn't hitting the webserver from the IP address: 123.456.789.123

like image 95
Jon Lin Avatar answered Oct 14 '22 22:10

Jon Lin