.htaccess根据IP将所有请求重写为子文件夹

时间:2017-11-08 16:19:28

标签: php apache .htaccess mod-rewrite url-rewriting

我正在尝试为我的服务器创建禁止监狱,我想将所有来自IP的请求重写为名为"禁止"的文件夹。从那里,几个PHP文件将处理禁令通知。我对脚本没有任何问题,但是当我尝试在.htaccess中使用重写规则时,我的浏览器只是警告我该页面没有正确重定向。这是我的代码

# Configure The Rules
Options +FollowSymLinks
RewriteEngine On

<If "%{REMOTE_ADDR} ='<insert banned IP here>'">
RewriteCond %{REQUEST_URI} !=/banned
RewriteRule .* /banned
</If>

我尝试了其他解决方案,但是他们要么导致500个错误,400个错误,要么填充了格式错误的请求,例如&#34; example.com/*If/banned//var/www/html/*If/banned...&#34;

请帮忙。

更新:使用curl -I: 'example.com'会产生以下结果:

HTTP/1.1 301 Moved Permanently
Date: Fri, 10 Nov 2017 19:22:27 GMT
Server: Apache
Strict-Transport-Security: max-age=63072000; includeSubDomains; preload
X-Frame-Options: SAMEORIGIN
X-Content-Type-Options: nosniff
Location: https://example.com/banned/
Content-Type: text/html; charset=iso-8859-1

1 个答案:

答案 0 :(得分:1)

请注意默认情况下使用mod_dir模块在​​目录后添加的尾部斜杠。

您可以使用此规则代替您的代码:

# Configure The Rules
Options +FollowSymLinks
RewriteEngine On

<If "%{REMOTE_ADDR} ='<insert banned IP here>'">
RewriteCond %{REQUEST_URI} !^/banned/ [NC]
RewriteRule ^ /banned/ [L]
</If>