简体   繁体   English

禁止直接访问index.php,但将每个URI重定向到index.php / URI

[英]Disable direct access to index.php but redirect every URI to index.php/URI

I want to disable direct access to /index.php but still use it as a front controller (using $_SERVER['PATH_INFO'] ). 我想禁用对/index.php直接访问,但仍将其用作前端控制器(使用$_SERVER['PATH_INFO'] )。 Meaning: 含义:

URI                    EXPECTED ACTION
-----------------------------------------------
/index.php             raise 404
/                      use   /index.php/
/foo/bar               use   /index.php/foo/bar
/index.php/foo/bar     raise 404

At the time I use the following .htaccess file: 当时我使用以下.htaccess文件:

RewriteEngine On
RewriteRule ^ index.php [QSA,L]

I do not have a need for any other file than index.php whatsoever (making an API). 除了index.php之外,我不需要任何其他文件(创建API)。

Thank you for your help! 谢谢您的帮助!

if you disable the access to any file, apache will be restrict it and if file have not an access so is disabled. 如果您禁用对任何文件的访问,则apache将对其进行限制,如果文件无访问权限,则被禁用。 You can use mod_rewrite to fake an url to display it index.php in URL, but you need to do other things with other file. 您可以使用mod_rewrite伪造一个URL以在URL中显示index.php,但是您需要对其他文件执行其他操作。

index.php -- not access
index.php/foo/bar -- no access
test.php -- have access
test.php/foo/bar -- you can fake it in url throw mod_rewrite and result will looks like as index.php/foo/bar

I hope so help ... 我希望有帮助...

Well I am not sure this will work but it is worth a try? 好吧,我不确定这是否行得通,但是值得一试吗? You could create a rewrite rule for anyone you enters the /index.php and rewrite it to an html file that does not exist on your server (foo.html or whatever). 您可以为输入/index.php的任何人创建一个重写规则,并将其重写为服务器上不存在的html文件(foo.html或其他内容)。 That should cause the 404 error when the file is not found but still allow the server to use its default behavior to load the index.php when no file name is specified. 当找不到文件时,这应该导致404错误,但是当未指定文件名时,仍允许服务器使用其默认行为来加载index.php。

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

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