编程语言
首页 > 编程语言> > php – .htaccess在子目录’覆盖’父htaccess

php – .htaccess在子目录’覆盖’父htaccess

作者:互联网

由于我对mod_rewrite缺乏了解以及对该项目的时间限制,所以一直在寻找2天并且无法得到正确的解决方案,因此希望有人可以提供帮助.

目的

如果客户端没有正确的cookie,则重写对根index.php的所有请求.
如果客户端具有正确的cookie,则允许他们按照自己的意愿进行浏览.

问题

我子目录中的htaccess优先于我的root htaccess,因此www.mydomain.com/subdir/index.php等请求不会被重定向.

我的根.htaccess

Options FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_COOKIE} !^.*pass.*$
RewriteCond %{REQUEST_URI} !^/index.php$
RewriteRule ^(.*)$http://www.mydomain.com/index.php?url=$0 [NC]

我的子公司htaccess

RewriteEngine On
RewriteBase /
RewriteCond $1 !\.(gif|jpe?g|png)$[NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$/index.php?/$1 [L]

附加信息

理想情况下,我正在尝试创建一个受密码保护的区域,因此所有请求都被路由到index.php,在这里可以输入密码,并在验证时创建cookie,允许自由浏览内容和子目录.所以,如果有更好的方法来实现这一点,那么请让我知道,因为我需要自定义登录,错误和启动页面,所以我没有去.htpasswd.

此外,subdir .htaccess是一个ExpressionEngine URL处理程序.

谢谢.

解决方法:

为了允许从父.htaccess(来自父文件夹的htaccess)执行重写规则,你需要明确地允许它(Apache会将当前.htaccess中的重写规则视为唯一需要执行的规则,只要重写的URL保留在相同的子文件夹).

您需要将此行添加到子文件夹中的.htaccess:

RewriteOptions inherit

Apache手册:http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriteoptions

标签:htaccess,php,mod-rewrite,rewrite,password-protection
来源: https://codeday.me/bug/20191001/1838856.html