简体   繁体   中英

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

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!!!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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