简体   繁体   English

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

[英].htaccess mod_rewrite rule “works”, but server sends 404 code!

I have the following .htaccess file on my webroot: 我的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]

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). 重写规则的目的是将对扩展名为.htm的页面的任何请求更改为对扩展名为.php的相同文件名的请求(除非实际上存在扩展名为.htm的文件)。 This rule seems to "work" in the sense that if I ask for a page with a .htm extension, I get the PHP content. 从某种意义上说,如果我要求使用扩展名为.htm的页面,那么此规则似乎“有效”,我可以获得PHP内容。 This is what I want, but at the same time HTTP header inspector tools report that the website is giving a 404 response. 这就是我想要的,但是同时HTTP标头检查器工具报告该网站给出了404响应。 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. 奇怪的是,我的工具报告在THE SAME RESPONSE主体中,服务器返回了PHP代码生成的正确HTML。 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!). 如果我强制服务器发送200代码([R = 200]),则会收到“内部服务器错误”响应,如果我强制服务器发送301代码([R = 301]),结果将是浏览器发出2个HTTP请求,而不是1个,无论如何,第二个请求最终还是一个404(但是我仍然在响应正文中获得了所需的HTML!)。 I'm confused!!! 我糊涂了!!!

Solved. 解决了。 The CMS software was sending a 404 header due to an error in the page code. 由于页面代码中的错误,CMS软件正在发送404标头。 Once I fixed that, I stopped getting a 404 code in the response header. 解决此问题后,我就停止在响应标头中获取404代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM