php - htaccess : different rewrite rules for different ip addresses

标签 php apache .htaccess mod-rewrite

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

我有这些规则:

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]

最后两条规则都可以正常工作,我可以选择启用哪一条。 我想以某种方式为自己启用一个规则,以便在开发版本上工作,并为生产启用其他规则。

我可以为此使用某种 IP 地址过滤吗?这可能吗?是否有任何其他替代解决方案,以便我可以同时启用两个规则,一个用于我的开发,另一个用于生产?

最佳答案

尝试:

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]

您需要复制重写条件。每组 RewriteCond 仅适用于紧接在 RewriteRule 之后的。所以第一条规则将适用于所有未从 IP 地址访问网络服务器的人:123.456.789.123

关于php - htaccess : different rewrite rules for different ip addresses,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19390594/

相关文章:

php - Google recaptcha 不能在我的网站上工作,但在其他网站上工作

php - 使用 AJAX 更改面板标题文本

使用 Jquery 的 Javascript 数组到 mysql 数据库

python - mail_admins 导致 Django/Apache2 连接被拒绝?

apache - .htaccess 仅从站点根目录重定向一级

php - 缓存,PHP 生成的缩略图加载缓慢

php - 在 mysql 中获取特定周的产品计数

java - Apache Olingo 的 jdbc 连接器

apache - Apache Proxy 背后的 Gitlab

php - 使用多域托管防止直接访问主域上的网站