.htaccess mod_rewrite 规则“有效”,但服务器发送 404 代码!

发布于 2024-09-24 00:36:00 字数 829 浏览 1 评论 0原文

我的 webroot 上有以下 .htaccess 文件:

DirectoryIndex index.htm index.html index.php AddType application/x-httpd-php .htm .html
RewriteEngine On
RewriteBase /

RewriteCond %{ENV:REDIRECT_STATUS} 200 RewriteRule .* - [L]

RewriteCond $1 !\.(gif|jpe?g|png|ico|css|js|pdf|mp3|flv|swf)$
RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/(.+)\.htm$ /$1.php [nc,L,QSA]

重写规则的目的是将具有 .htm 扩展名的页面的任何请求更改为对具有 .php 扩展名的相同文件名的请求(除非具有 .htm 扩展名的文件)确实存在)。这条规则似乎“有效”,因为如果我请求一个带有 .htm 扩展名的页面,我就会得到 PHP 内容。这就是我想要的,但同时 HTTP 标头检查器工具报告该网站正在给出 404 响应。奇怪的是,我的工具报告说,在“相同响应”的正文中,服务器返回了由 PHP 代码生成的正确 HTML。这似乎是一个矛盾:找不到页面,但这里是您想要的内容?

如果我强制服务器发送 200 代码 ([R=200]),我会收到“内部服务器错误”响应,如果我强制服务器发送 301 代码 ([R=301]),结果是浏览器发出了 2 个 HTTP 请求,而不是 1 个,而第二个请求最终还是 404(但我仍然在响应正文中获得了所需的 HTML!)。我很困惑!!!

I have the following .htaccess file on my webroot:

DirectoryIndex index.htm index.html index.php AddType application/x-httpd-php .htm .html
RewriteEngine On
RewriteBase /

RewriteCond %{ENV:REDIRECT_STATUS} 200 RewriteRule .* - [L]

RewriteCond $1 !\.(gif|jpe?g|png|ico|css|js|pdf|mp3|flv|swf)$
RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/(.+)\.htm$ /$1.php [nc,L,QSA]

The purpose of the Rewrite rule is to change any request for a page with a .htm extension to a request for the same filename with a .php extension (unless the file with .htm extension actually exists). This rule seems to "work" in the sense that if I ask for a page with a .htm extension, I get the PHP content. This is what I want, but at the same time HTTP header inspector tools report that the website is giving a 404 response. What's weird is that my tools report that in the body of THE SAME RESPONSE, the server returns the correct HTML generated by the PHP code. That seems like a contradiction: page not found, but here's the content you wanted anyway??

If I force the server to send a 200 code ([R=200]), I get an "Internal server error" response, and if I force it to send a 301 code ([R=301]), the result is that the browser makes 2 HTTP requests instead of 1, and the second one ends up being a 404 anyway (but I still get the desired HTML in the response body!). I'm confused!!!

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

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

发布评论

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

评论(1

不一样的天空 2024-10-01 00:36:00

解决了。由于页面代码错误,CMS 软件正在发送 404 标头。修复此问题后,我就不再在响应标头中收到 404 代码。

Solved. The CMS software was sending a 404 header due to an error in the page code. Once I fixed that, I stopped getting a 404 code in the response header.

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