使用htaccess屏蔽目录并阻止原始目录

时间:2014-04-17 09:48:22

标签: regex apache .htaccess mod-rewrite

我使用以下代码来屏蔽目录;

Options +FollowSymlinks
RewriteEngine On

RewriteRule libs(.*) wp-content/$1 [L]

如果请求原始目录,我希望服务器显示404错误,但如果使用了屏蔽URL,则允许访问。

例如;

用户在浏览器中访问/wp-content/something.html并收到404错误, 但如果用户改为/libs/something.html,则文件显示正确。

此规则需要适用于除localhost之外的所有人。

我已尝试过这段代码,但是当掩码发生时,请求uri被更改后,它无法正常工作。

RewriteCond %{HTTP_HOST} !=localhost
RewriteCond %{REQUEST_URI} wp-content
RewriteRule ^wp-content/ - [L,R=404]

1 个答案:

答案 0 :(得分:1)

使用THE_REQUEST代替表示对Apache的原始请求:

RewriteCond %{HTTP_HOST} !=localhost
RewriteCond %{THE_REQUEST} wp-content
RewriteRule ^ - [L,R=404]
相关问题