简体   繁体   English

.htaccess 将 REQUEST_METHOD 更改为 GET - 如果 URL 没有参数或尾部斜杠

[英].htaccess changes REQUEST_METHOD to GET - if the URL does not have params or a trailing slash

I'm building an api eg.我正在构建一个 api 例如。 https://example.com/my-api/api.php

Then I use the following .htaccess file to make the api usable via https://example.com/my-api然后我使用以下 .htaccess 文件通过https://example.com/my-api使 api 可用

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

The file works, however when my api-call doesn't end with a trailing slash or has some parameters set, the $_SERVER['REQUEST_METHOD'] becomes GET.该文件有效,但是当我的 api 调用没有以斜杠结尾或设置了一些参数时,$_SERVER['REQUEST_METHOD'] 变为 GET。

Examples:例子:

POST https://example.com/my-api/foo #becomes GET
POST https://example.com/my-api/foo/ #stays POST
POST https://example.com/my-api/foo?bar=1 #stays POST

POST https://example.com/my-api/foo/bar #becomes GET
POST https://example.com/my-api/foo/bar/ #stays POST
POST https://example.com/my-api/foo/bar?abc=1 #stays POST

How could I change this behavior (and is this desirable) or should I inform the users of this api to always end their calls with a trailing slash?我怎样才能改变这种行为(这是可取的)还是我应该通知这个 api 的用户总是以斜杠结束他们的通话? When the trailing slash is added, the api works as expected.添加尾部斜杠后,api 按预期工作。

I tested this behaviour mostly via PostMan.我主要通过 PostMan 测试了这种行为。

It is happening because of a missing slash in your URI which points to a real directory.发生这种情况是因为您的 URI 中缺少指向真实目录的斜杠。 Apache's mod_dir module adds a trailing slash for those requests and performs a 301 redirect that results in another GET request. Apache 的mod_dir模块为这些请求添加尾部斜杠并执行301重定向,从而导致另一个GET请求。

You can disable this behavior but also important to disable option to list directory content for security reasons.您可以禁用此行为,但出于安全原因,禁用列出目录内容的选项也很重要。

DirectorySlash Off
Options -Indexes

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

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

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