htaccess 重写规则,[R,L = 301] 标志不会阻止它。为什么?

发布于 2025-01-02 23:09:26 字数 541 浏览 2 评论 0原文

.htaccess 的开头是

RewriteEngine On

#begin of rules for administration folder, redirect to https, if not https
RewriteCond %{REQUEST_URI} ^/administration
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R,L=301]
#end of rules for administration folder

#===no rules for /administration folder below this line===
#the rest part of .htaccess

为什么 .htaccess 的其余部分仍然执行 https://www.mydomain.com/administration/index.php ?如何停止执行管理文件夹后面的 .htaccess 文件的其余部分?我的代码有什么问题吗? 谢谢。

The beginning of .htaccess is

RewriteEngine On

#begin of rules for administration folder, redirect to https, if not https
RewriteCond %{REQUEST_URI} ^/administration
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R,L=301]
#end of rules for administration folder

#===no rules for /administration folder below this line===
#the rest part of .htaccess

Why does the rest part of .htaccess still performs for https://www.mydomain.com/administration/index.php ? How to stop the performing of the rest part of .htaccess file for urls that follow to administration folder? What's wrong in my code?
Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

野鹿林 2025-01-09 23:09:26
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
西瑶 2025-01-09 23:09:26

将此行添加到 .htaccess 文件顶部:

RewriteRule ^administration - [NC,L]

Add this line on top of your .htaccess file:

RewriteRule ^administration - [NC,L]
千紇 2025-01-09 23:09:26

这些天我遇到了类似的问题,并且在 PHP 聊天中从用户 DaveRandom 那里得到了一个非常有趣的答案:

“基本上,mod_rewrite 有效地在循环中运行(这不是真的,但实际上这就是它的作用)和 [L] flag 就像 PHP 中的“继续”语句(它在当前迭代中停止处理下面的规则,但随后它将再次从顶部开始处理规则),因此在您的规则中,第一次迭代匹配第一个规则,并且那么第二次迭代跳过第一条规则(它产生与输入相同的输出),然后匹配第二条规则。”

可能的解决方案:“如果您使用的是 apache 2.4,则使用 [END] 而不是 [L] 可以修复”。

I had simmilar issue these days and I got a really interesting answer from the user DaveRandom in the PHP chat:

"Basically, mod_rewrite effectively runs in a loop (this isn't really true but effectively that's what it does) and the [L] flag is like a "continue" statement in PHP (it stops processing the rules below in the current iteration but it will then start processing the rules again from the top). So in your rules, the first iteration was matching the first rule, and then the second iteration skipped the first rule (it produced the same output as the input) and then matched the second rule."

Possible Solution: "if you are using apache 2.4 using [END] instead of [L] would be a fix".

冰之心 2025-01-09 23:09:26

这是因为你有语法错误。 .htaccess 将忽略错误行

更改此:-

RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R,L=301]

更改为:-

RewriteRule .? https://%{HTTP_HOST}%{REQUEST_URI} [R=301]

It is because you have syntax error. .htaccess will ignore erroring lines

Change this:-

RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R,L=301]

To this:-

RewriteRule .? https://%{HTTP_HOST}%{REQUEST_URI} [R=301]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文