htaccess的重写导致500错误,而不是404重写、而不是、错误、htaccess

2023-09-02 00:36:34 作者:裤裆里有货不信你摸摸

我最近增加的code这一点给我的.htaccess

I've recently added this little bit of code to my .htaccess

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L,QSA]

好吧,我知道这里发生了什么,我想。的code这一点,除去PHP文件扩展名会导致一个循环,如果没有找到该文件。这个循环将导致500服务器错误,而不是(正确)404。不幸的是我有什么是地狱,这些重写实际上是做了解甚少,所以我不知道如何将它改写到只有触发此重定向,如果该文件存在,

Ok, I understand what's happening here, I think. This little bit of code to remove php file extensions causes a loop if the document is not found. This loop causes a 500 server error instead of the (proper) 404. Unfortunately I have very little understanding of what the hell these rewrites are actually doing, so I don't know how to rewrite it to only trigger this redirect if the document exists.

我已经做了一些阅读,我不知道是什么阿帕奇认为正规的文件。我的意思是它的工作原理,但为什么不第一行是-f而不是!-f?是-u做到这一点的唯一途径?

I've done some reading and I'm not sure what Apache considers a "regular" file. I mean it works, but why wouldn't the first line be "-f" instead of "!-f"? Is -u the only way to accomplish this?

任何帮助吗?

推荐答案

您应该如果新的路径指向测试到现有文件:

You should test if new path points to an existing file:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L,QSA]

否则,你会得到一个无限递归(错误500)。

Otherwise you will get an infinite recursion (error 500).