禁用特定文件夹的 Htaccess
我在 .htaccess
(位于根目录中)中有此规则
RewriteCond %{REQUEST_METHOD} !POST [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
,并且一切正常。
现在,我想仅对文件夹等禁用此规则。/testfolder
。 所以我想在 /testfolder
内进行 php POST 时禁用主 .htaccess
文件中的规则。
I have this rule in .htaccess
(located in root)
RewriteCond %{REQUEST_METHOD} !POST [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
and all is working fine.
Now, I would like to disable this rule only for folder etc. /testfolder
.
So I would like when make php POST inside /testfolder
to disable rules from main .htaccess
file.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在一般情况下,除非您有权访问服务器配置,否则您不能简单地“禁用特定文件夹的 htaccess”。
但是,您可以通过在特定文件夹(和子文件夹)中放置
.htaccess
来禁用特定文件夹(和子文件夹)中的 mod_rewrite (这就是您在此处使用的内容):这将阻止在父文件夹中使用 mod_rewrite 指令正在处理配置。但是,来自其他模块(例如 mod_alias、mod_headers 等)的指令仍将被处理。
为特定规则设置例外
但是,您只有一条不希望应用于特定文件夹的规则。在这种情况下,您可以创建一个额外的条件(
RewriteCond
指令)来排除此文件夹。例如:
!
前缀对表达式取反,所以不匹配就成功。换句话说,当请求的 URL 不是/testfolder
或不以/testfolder/
开头时,第一个条件成功。注意:我删除了 RewriteRule 指令中的 NC 标志,因为这里不需要它。
但是,由于您希望此规则应用于除此子文件夹之外的所有请求,因此直接在
RewriteRule
指令。例如:无论如何,上面的规则仅适用于非 POST 请求(如
!POST
所示),因此您不需要一开始就在此规则中做出例外,所以可能还有另一个潜在问题?但是,如果您在整个网站中使用无扩展名 URL(隐藏
.php
扩展名),那么您不应向.php
提交表单 > URL 版本。因此,这里不需要例外。In the general case you can't simply "disable htaccess for a specific folder" unless you have access to the server config.
However, you can disable mod_rewrite (which is what you are using here) in a specific folder (and subfolders) by placing a
.htaccess
in that folder with the following:This will prevent mod_rewrite directives in the parent config being processed. However, directives from other modules (eg. mod_alias, mod_headers, etc.) will still be processed.
Make an exception for a specific rule
However, you only have a single rule that you don't want to apply to a particular folder. In this case you can create an additional condition (
RewriteCond
directive) that excludes this folder.For example:
The
!
prefix negates the expression, so it is successful when it does not match. In other words, the first condition is successful when the requested URL is not/testfolder
or does not start with/testfolder/
.NB: I removed the
NC
flag in theRewriteRule
directive since it's not required here.However, since you want this rule to apply to all requests except this one subfolder, it would be marginally more efficient (and 1 less directive) to perform this check directly in the
RewriteRule
directive. For example:The rule above only applies to non-POST requests anyway (as denoted by
!POST
), so you shouldn't need to make an exception in this rule to begin with, so maybe there is another underlying issue?However, if you are using extensionless URLs throughout your site (hiding the
.php
extension) then you shouldn't be submitting forms to the.php
version of the URL. So, again, an exception should not be required here.