简单的重写规则问题

发布于 2024-09-18 05:46:54 字数 600 浏览 2 评论 0原文

这看起来很简单,但我无法弄清楚...我希望将所有请求重写为index.php(它将解释请求)除了 404.html 应该是直接重写为404.php。

以下代码确实可以将所有内容重写到index.php,但404.php永远不会被触发。

我对第 3 行留下了评论,因为我不知道它的作用。

Options +FollowSymlinks
RewriteEngine on
#RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^404.html$ 404.php [L]
RewriteRule ^(.*)$ index.php  [QSA]

正如你所看到的,我的需求很简单,但我的大脑也很简单!请帮忙!

谢谢。

编辑

这行代码本身工作得很好:

RewriteRule ^404.html$ 404.php

但是当引入其他 RewriteRule 时,所有内容都将通过index.php 强制进行 - 甚至 404.html......(我也尝试更改顺序) 。

This seems so simple, but I can't figure it out... I would like all requests to be rewritten to index.php (which will interpret the request) apart from 404.html which should be rewritten directly to 404.php.

The following code does work at rewriting everything to index.php, but 404.php never gets triggered.

I have left line 3 commented because I don't know what it does.

Options +FollowSymlinks
RewriteEngine on
#RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^404.html$ 404.php [L]
RewriteRule ^(.*)$ index.php  [QSA]

As you can see, my needs are very simple, but so is my brain! Please help!

Thanks.

EDIT

This line works fine on its own:

RewriteRule ^404.html$ 404.php

but when the other RewriteRule is introduced, everything gets forced through index.php - even 404.html...... (I tried changing the order too).

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

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

发布评论

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

评论(3

末蓝 2024-09-25 05:46:54

您可以反转匹配结果并在其前面添加 !。因此:

RewriteRule ^404\.html$ 404.php
RewriteRule !^404\.html$ index.php [QSA]

这将匹配路径与 ^404\.html$ 不匹配的任何请求。

您可能需要添加注释掉的条件:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !^404\.html$ index.php [QSA]

现在,仅当请求无法另外映射到现有文件时,此规则才会匹配。

You can invert the result of a match with prepend it with !. So:

RewriteRule ^404\.html$ 404.php
RewriteRule !^404\.html$ index.php [QSA]

This will match any request that’s path is not matched by ^404\.html$.

You might need to add the condition that is commented out:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !^404\.html$ index.php [QSA]

Now this rule will only match if the request can additionally not be mapped onto an existing file.

我的痛♀有谁懂 2024-09-25 05:46:54

我建议这个解决方案:

Options +FollowSymlinks
RewriteEngine on
#RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^404.html$ 404.php [L]
RewriteRule ^(.*)$ index.php  [QSA]

我相信问题是您将 URL 重写为 404.php,然后将其发送到 index.php。在第一条规则后面加上 [L] 将指示重写引擎不继续重写。

I suggest this solution:

Options +FollowSymlinks
RewriteEngine on
#RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^404.html$ 404.php [L]
RewriteRule ^(.*)$ index.php  [QSA]

I believe the issue is that you are rewriting the URL to 404.php, then sending it to index.php. Putting [L] after the first rule will instruct the rewrite engine to not continue rewriting.

听,心雨的声音 2024-09-25 05:46:54

这让我疯狂!,但对于那些像我一样感到困惑的人来说,我找到了答案。

查看第 4 行:

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^404.html$ 404.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php

取自:easymodrewrite

如果您不清楚该模块的行为方式。请注意,以下内容仅适用于在 .htaccess 文件中使用 mod_rewrite 时。 [L] 标志在 httpd.conf 中使用时的行为与预期完全一致。

L 标志将告诉 Apache 停止处理该请求的重写规则。现在经常没有意识到的是,它现在对新的重写文件名发出新请求,并再次开始处理重写规则。


基本上,您必须告诉第二条规则不要匹配现有文件(404.php 是真实文件,而不是动态文件)。

希望这对某人有帮助。

This was driving me MAD! but I found the answer, for those who are getting confused like I was.

Look at line 4:

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^404.html$ 404.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php

Taken from: easymodrewrite:

The [L] flag can have unexpected results if you are not clear on how the module behaves. Note that the following only applies when mod_rewrite is used in a .htaccess file. The [L] flag behaves exactly as expected when used in httpd.conf.

The L flag will tell Apache to stop processing the rewrite rules for that request. Now what is often unrealised is that it now makes a new request for the new, rewritten filename and begin processing the rewrite rules again.


Basically, you have to tell the second rule not to match existing files (404.php is a real file, not dynamic).

Hope this helps someone.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文