php 子目录“覆盖”父 htaccess 中的 .htaccess
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7337431/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
.htaccess in subdirectory 'overriding' parent htaccess
提问by Abe
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.
由于我缺乏对 mod_rewrite 的理解和这个项目的时间限制,我已经搜索了 2 天并不能完全得到正确的解决方案,所以希望有人能提供帮助。
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.
如果客户端没有正确的 cookie,则将所有请求重写到根 index.php。如果客户端拥有正确的 cookie,则允许他们随意浏览。
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.
我的子目录中的 htaccess 优先于我的根 htaccess,因此诸如 www.mydomain.com/subdir/index.php 之类的请求不会被重定向。
My root .htaccess
我的根 .htaccess
Options FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_COOKIE} !^.*pass.*$
RewriteCond %{REQUEST_URI} !^/index.php$
RewriteRule ^(.*)$ http://www.mydomain.com/index.php?url=RewriteEngine On
RewriteBase /
RewriteCond !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/ [L]
[NC]
My subdir htaccess
我的子目录 htaccess
RewriteOptions inherit
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.
理想情况下,我正在尝试创建一个受密码保护的区域,因此所有请求都被路由到 index.php,可以在其中输入密码,并在验证后创建 cookie,允许自由浏览内容和子目录。因此,如果有更好的方法来完成此操作,请告诉我,我没有选择 .htpasswd,因为我需要自定义登录、错误和启动页面。
Also, the subdir .htaccess is an ExpressionEngine URL handler.
此外,子目录 .htaccess 是 ExpressionEngine URL 处理程序。
Thanks.
谢谢。
回答by LazyOne
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).
要允许从父 .htaccess 执行重写规则(来自父文件夹的 htaccess),您需要明确允许它(Apache 会将当前 .htaccess 中的重写规则视为唯一需要执行的规则,只要重写的 URL 保留在相同的子文件夹)。
You need to add this line to your .htaccess in sub-folder:
您需要将此行添加到子文件夹中的 .htaccess 中:
##代码##Apache manual: http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriteoptions
Apache 手册:http: //httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriteoptions