不同的ip地址的重写规则。

时间:2021-09-08 11:19:57

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

是否可能只使用一个.htaccess文件对不同的IP地址应用不同的重写规则?

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? Thanks!

我可以用IP地址过滤吗?甚至可能吗?有没有其他替代的解决方案可以让我同时启用两个规则,一个用于开发,另一个用于生产?谢谢!

2 个解决方案

#1


3  

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

您需要复制重写条件。每一套RewriteCond只适用于紧随其后的RewriteRule。因此,第一条规则将适用于所有没有通过IP地址123.456.789.123访问webserver的用户

#2


0  

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]

#1


3  

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

您需要复制重写条件。每一套RewriteCond只适用于紧随其后的RewriteRule。因此,第一条规则将适用于所有没有通过IP地址123.456.789.123访问webserver的用户

#2


0  

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]