htaccess保护目录,通过ip重定向

时间:2017-06-14 20:22:14

标签: apache .htaccess redirect mod-rewrite

这里是我的htaccess文件

/。htaccess的

<IfModule autoindex>
  IndexIgnore *
</IfModule>

Options +FollowSymlinks
Options -Indexes

RewriteEngine On

RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

RewriteCond %{QUERY_STRING} base64_encode[^(]*\([^)]*\) [OR]
RewriteCond %{QUERY_STRING} (<|%3C)([^s]*s)+cript.*(>|%3E) [NC,OR]
RewriteCond %{QUERY_STRING} GLOBALS(=|\[|\%[0-9A-Z]{0,2}) [OR]
RewriteCond %{QUERY_STRING} _REQUEST(=|\[|\%[0-9A-Z]{0,2})
RewriteRule .* index.php [F]

RewriteBase /

RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L]

/secret/.htaccess

RewriteEngine On
RewriteBase /
RewriteCond %{REMOTE_ADDR} !=98.81.105.12
RewriteCond %{REQUEST_URI} !%{HTTP_HOST}/secret/$
RewriteRule .* %{HTTP_HOST} [R=302,L]

因此,目标是使用IP过滤器保护https://example.com/secret/并重定向到https://example.com,甚至更好地显示404错误&#34;未找到&#34; 我的解决方案不起作用。任何的想法? 提前致谢。

1 个答案:

答案 0 :(得分:0)

最终的RewriteRule最好重定向到“/”而不是%{HTTP_HOST}。该变量的值没有scheme / port / URL,因此实际上并不期望它是重定向的目标。