.htaccess 在子目录“覆盖”中父级访问
由于我对 mod_rewrite 缺乏了解以及该项目的时间限制,我已经搜索了 2 天,但无法完全找到正确的解决方案,因此希望有人可以提供帮助。
目标
如果客户端没有正确的cookie,则重写对根index.php的所有请求。 如果客户端拥有正确的 cookie,则允许他们按照自己的意愿进行浏览。
问题
我的子目录中的 htaccess 优先于我的根 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.html。 php 中可以输入密码,验证后会创建 cookie,允许自由浏览内容和子目录。因此,如果有更好的方法来完成此任务,请告诉我,我没有选择 .htpasswd,因为我需要自定义登录、错误和启动页面。
此外,子目录 .htaccess 是一个 ExpressionEngine URL 处理程序。
谢谢。
been searching for 2 days and can't quite get the right solution due to my lack of understanding of mod_rewrite and time constraints on this project so hoping someone can help.
The aim
To rewrite all requests to the root index.php if the client doesn't have the correct cookie.
If the client has the correct cookie allow them to browse as they wish.
The problem
The htaccess in my subdirectory is taking precendence over my root htaccess, so requests such as www.mydomain.com/subdir/index.php arn't getting redirected.
My root .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]
My subdir htaccess
RewriteEngine On
RewriteBase /
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
Additional info
Ideally I'm trying to create a password protected area, so all requests are routed to index.php where a password can be entered and when verified a cookie is created, allowing free browsing of contents and sub directories. So if there is a better way to accomplish this then please let me know, and I havn't gone for .htpasswd since I need custom login, error and splash pages.
Also, the subdir .htaccess is an ExpressionEngine URL handler.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要允许执行来自父 .htaccess 的重写规则(来自父文件夹的 htaccess),您需要明确允许(只要重写的 URL 保留在其中,Apache 就会将当前 .htaccess 中的重写规则视为唯一需要执行的规则)相同的子文件夹)。
您需要将此行添加到子文件夹中的 .htaccess:
Apache 手册: http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriteoptions
To allow execution of rewrite rules from parent .htaccess (htaccess from parent folder), you need to explicitly allow it (Apache will treat rewrite rules in current .htaccess as the only one that need to be executed, as long as rewritten URL remains in the same subfolder).
You need to add this line to your .htaccess in sub-folder:
Apache manual: http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriteoptions