如何在没有尾部斜杠的情况下访问目录的 index.php 并且不会获得 301 重定向 [英] How to access directory's index.php without a trailing slash AND not get 301 redirect

查看:18
本文介绍了如何在没有尾部斜杠的情况下访问目录的 index.php 并且不会获得 301 重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个结构:site.com/api/index.php.当我将数据发送到 site.com/api/ 时没有问题,但我想如果 api 也可以在没有尾部斜杠的情况下工作会更好,如下所示:site.com/api.这会导致 301 重定向并因此丢失数据(因为数据未转发).我尝试了所有我能想到的重写,但无法避免重定向.这是我目前的重写规则(尽管它可能无关紧要).

I have this structure: site.com/api/index.php. When I send data to site.com/api/ there is no issue, but I imagine it would be better if the api would work without the trailing slash also, like this: site.com/api. This causes a 301 redirect and thus loses the data (since data isn't forwarded). I tried every re-write I could think of and couldn't avoid the redirect. This is my current re-write rule (though it may be irrelevant).

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^api/(.*)$ api/index.php [L]

我可以在不使用尾部斜杠的情况下使这个 url 工作并维护帖子数据吗?

Can I make this url work and maintain the post data without using the trailing slash?

一些无效的重写:(仍然重定向)

Some rewrites that didn't work: (all still redirect)

RewriteRule ^api$ api/index.php [L] 
RewriteRule ^api/*$ api/index.php [L]

推荐答案

您首先需要关闭目录斜杠,但有一个结尾斜杠非常重要是有原因的:

You'd first need to turn off directory slash, but there's a reason why it is very important that there's a trailing slash:

Mod_dir 文档:

关闭尾部斜杠重定向可能会导致信息泄露.考虑一种情况,其中 mod_autoindex 处于活动状态 (Options +Indexes) 并且 DirectoryIndex 设置为有效资源(例如,index.html)并且有没有为该 URL 定义其他特殊处理程序.在这种情况下,带有尾部斜杠的请求将显示 index.html 文件.但没有尾部斜杠的请求将列出目录内容.

Turning off the trailing slash redirect may result in an information disclosure. Consider a situation where mod_autoindex is active (Options +Indexes) and DirectoryIndex is set to a valid resource (say, index.html) and there's no other special handler defined for that URL. In this case a request with a trailing slash would show the index.html file.But a request without trailing slash would list the directory contents.

这意味着访问不带斜杠的目录只会列出目录内容,而不是提供默认索引(例如 index.php).因此,如果您想关闭目录斜杠,则必须确保在内部将尾部斜杠重新写入.

That means accessing a directory without a trailing slash will simply list the directory contents instead of serving the default index (e.g. index.php). So if you want to turn directory slash off, you have to make sure to internally rewrite the trailing slash back in.

DirectorySlash Off

RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*[^/])$ /$1/

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^api/(.*)$ api/index.php [L]

第一条规则确保尾部斜杠被附加到末尾,尽管只是在内部.与外部重定向浏览器的 mod_dir 不同,内部重写对浏览器是不可见的.然后下一条规则执行 api 路由,并且由于第一条规则,保证有一个尾部斜杠.

The first rule ensures that the trailing slash gets appended to the end, though only internally. Unlike mod_dir, which externally redirects the browser, the internal rewrite is invisible to the browser. The next rule then does the api routing, and because of the first rule, there is guaranteed to be a trailing slash.

这篇关于如何在没有尾部斜杠的情况下访问目录的 index.php 并且不会获得 301 重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆