如何访问没有斜线目录的index.php文件,并没有得到301重定向 [英] How to access directory's index.php without a trailing slash AND not get 301 redirect

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

问题描述

我有这样的结构: site.com/api/index.php 。当我将数据发送到 site.com/api / 没有问题,但我想它会更好,如果API将工作没有最后的斜线,也就像这样: site.com/api 。这会导致一个301重定向并因此丢失数据(由于数据是不转发)。我想尽一切重新写我能想到的和无法避免的重定向。这是我目前的重写规则(尽管它可能是无关紧要的)。

 的RewriteCond%{} REQUEST_FILENAME!-f
的RewriteCond%{} REQUEST_FILENAME!-d
重写规则^ API /(.*)$ API / index.php文件[L]
 

我可以做这个网址的工作和维护数据后不使用尾随斜线?

有些重写,没有工作:(都还是重定向)

 重写规则^ API $ API / index.php文件[L]
重写规则^ API / * $ API / index.php文件[L]
 

解决方案

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

Mod_dir文档

  

关闭斜杠重定向可能会导致信息泄露。考虑这样的情况mod_autoindex活跃(选项+指标)的DirectoryIndex 设置为一个有效的资源(例如, index.html的),并有该URL定义没有其他特殊的处理。在这种情况下,以斜线的请求将呈现 index.html的文件。但是,如果没有结尾的斜线将列出目录内容的请求。

这意味着访问的目录中没有斜线将简单地列出目录的内容,而不是服务于默认的指数(例如:的index.php )。所以,如果你想打开目录削减了,你必须确保在内部改写结尾的斜线回

  DirectorySlash关闭

的RewriteCond%{} REQUEST_FILENAME -d
重写规则^(。* [^ /])/ $ 1 /

的RewriteCond%{} REQUEST_FILENAME!-f
重写规则^ API /(.*)$ API / index.php文件[L]
 

第一条规则确保了最后的斜线被追加到最后,虽然只在内部。不像mod_dir,其中外部浏览器重定向,内部重写是不可见的浏览器。下一条规则然后进行API路由,因为第一个规则的,有保证是尾部斜杠。

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]

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 docs:

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.

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]

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天全站免登陆