Mod_Rewrite隐藏目录

时间:2013-08-20 21:50:38

标签: php regex apache .htaccess mod-rewrite

我对mod_rewrite规则没什么问题。该规则似乎正在起作用,但阻止访问目录及其文件。

这是我想要完成的事情

www.example.com/about-us/  *** Parent rule

www.example.com/about-us/our-mission.html *** child rule

虽然,我的规则是完成此操作但阻止访问服务器上的物理文件和目录。

在同一台服务器上,我有管理界面,用户需要访问该界面才能进行更改......

www.example.com/manage/dash.php

当我尝试访问目录/ manage或它是文件时,我被重定向到404页面,该页面是由子重写规则生成的重定向。

当我注释掉子重写规则时,我能够访问这些提到的但是使用子规则,访问被阻止。

请参阅下面的重写代码并帮我确定问题。

 RewriteEngine On
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d


 #This rule will rewrite url to the main page www.example.com/about-us
 RewriteRule ^([^/]+)/?$ index.php?get_parent=$1 [L]

#This rule  will rewrite url to the secondary url www.example.com/abou-us/our-mission.html
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?get_parent=$1&get_child=$2 [L]

谢谢。

2 个答案:

答案 0 :(得分:0)

您还需要从2nd RewriteRule中排除文件/目录。

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#This rule will rewrite url to the main page www.example.com/about-us
RewriteRule ^([^/]+)/?$ index.php?get_parent=$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#This rule  will rewrite url to the secondary url www.example.com/abou-us/our-mission.html
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?get_parent=$1&get_child=$2 [L]

答案 1 :(得分:0)

这看起来很公平,因为您的网址www.example.com/manage/dash.php与正则表达式相匹配。使用第一个规则末尾的[L],第二个规则不再使用2个条件

您也应添加不匹配的模式(请参阅documentation

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

这里我们假设文件/manage/dash.php确实存在。

希望它会对你有所帮助。