简体   繁体   English

文件名斜线时,.htaccess返回500 Internal Server Error

[英].htaccess returns 500 Internal Server Error when slash on filename

My .htaccess file has a rewrite in it so instead of seeing example.com/login.php , one will see example.com/login . 我的.htaccess文件中有一个重写文件,因此不会看到example.com/login.php ,而是看到example.com/login

Here is what is written in that file: 这是该文件中写入的内容:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

It works perfectly except for one thing: I am creating a file called api.php that will serve as the api. 除了一件事情外,它工作得很好:我正在创建一个名为api.php的文件,它将用作api。 I want clients to go to example.com/api/logout and example.com/api/auth etc. even though api is not a folder but instead a PHP file. 我希望客户端转到example.com/api/logoutexample.com/api/auth等,即使api不是文件夹而是PHP文件。

Problem is, whenever I go to a file like example.com/api/logout (or even example.com/login/foo ), I always get a 500 Internal Server Error. 问题是,每当我转到example.com/api/logout (甚至example.com/login/foo )之类的文件时,总是会收到500 Internal Server Error。 If I go to example.com/api.php/logout , I get a 404 error. 如果我转到example.com/api.php/logoutexample.com/api.php/logout收到404错误。

I checked the error file for the 500 Internal Server Error, and it says Request exceeded the limit of 10 internal redirects due to probable configuration error which makes sense because mod_rewrite runs until all conditions are satisfied. 我检查了错误文件中的500 Internal Server Error,它说Request exceeded the limit of 10 internal redirects due to probable configuration errorRequest exceeded the limit of 10 internal redirects due to probable configuration error这是有道理的,因为mod_rewrite运行直到满足所有条件。

How should I change my rewrite to fix this? 我应该如何更改重写以解决此问题?

 : RewriteCond %{REQUEST_FILENAME}.php -f RewriteRule ^(.+?)/?$ $1.php [L] 

When you request a URL of the form /api/logout you will get a rewrite loop (500 error) because the REQUEST_FILENAME maps to <document-root>/api (which if you append .php is indeed a valid file), but you end up appending .php to /api/logout (the URL-path) in the RewriteRule directive (clearly not the same thing). 当您请求格式为/api/logout的URL时,将得到一个重写循环(500错误),因为REQUEST_FILENAME映射到<document-root>/api (如果附加.php确实是有效文件),但是您最终在RewriteRule指令中将.php附加到/api/logout (URL路径)(显然不是同一回事)。 /api/logout.php then gets rewritten to /api/logout.php.php etc. etc. repeatedly appending .php to the URL-path, while all the time checking that /api.php is a valid file. /api/logout.php再被改写, /api/logout.php.php等等等等反复追加.php的URL路径,而所有的时间检查/api.php是一个有效的文件。

If all your files are in the document root, then you can change your RewriteRule pattern to only match the first path segment. 如果所有文件都在文档根目录中,则可以将RewriteRule 模式更改为仅与第一个路径段匹配。 For example: 例如:

RewriteRule ^([^/]+) $1.php [L]

However, if you have files at different directory depths (which you are referencing without the .php extension) then you might need to create some specific rules / exceptions. 但是,如果文件位于不同的目录深度(您引用的.php没有.php扩展名),则可能需要创建一些特定的规则/例外。

If I go to example.com/api.php/logout , I get a 404 error. 如果我转到example.com/api.php/logoutexample.com/api.php/logout收到404错误。

This will result in a 404 if additional pathname information (PATH_INFO) has been disabled (although it is usually enabled by default for PHP files). 如果禁用了其他路径名信息 (PATH_INFO),则将导致404(尽管通常默认情况下已为PHP文件启用)。 You can explicitly enable this at the top of your .htaccess file: 您可以在.htaccess文件顶部显式启用此功能:

AcceptPathInfo On

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

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