使用 .htaccess 强制删除 index.php
我目前正在使用以下内容重写 http://www.site.com/index.php/test/
,也可以直接与 http://www.site.com/ 一起使用test/
,但我不仅想允许第二个版本,我还想强制使用第二个版本。如果用户访问http://www.site.com/index.php/test/
,它应该立即将其重新路由到http://www.site.com/test/< /代码>。 index.php 永远不应出现在 url 中。规定:这只适用于第一个index.php。如果我有一个像
http://www.site.com/index.php/2011/06/08/remove-index.php-from-urls/
这样的标题,它应该保留第二个索引。 php,因为它是 URL 的一部分。
目前允许但不强制的规则: #删除index.php RewriteCond $1 !^(index.php|images|css|js|robots.txt) RewriteRule ^(.*)$ /index.php/$1 [L]
谢谢。
I'm currently using the following to rewrite http://www.site.com/index.php/test/
to also work directly with http://www.site.com/test/
, but I would like to not only allow the second version, I would like to FORCE the second version. If a user goes to http://www.site.com/index.php/test/
it should immediately reroute them to http://www.site.com/test/
. index.php should never appear in a url. Stipulation: this should only apply to the first index.php. If I have a title like http://www.site.com/index.php/2011/06/08/remove-index.php-from-urls/
it should leave the second index.php, as it is part of the URL.
Current rule that allows but does not force:
#Remove index.php
RewriteCond $1 !^(index.php|images|css|js|robots.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如您所写,如果用户访问
http://www.site.com/index.php/test/
,此规则将立即将他重新路由到http://www.site。 com/test/
我不确定这是否是您所需要的,因为您当前的重写规则与我的相反。
As you wrote, if a user goes to
http://www.site.com/index.php/test/
this rule will imediately reroute him tohttp://www.site.com/test/
I'm not sure if that is what you need as your current rewrite rule is opposite to mine.
第一个(也是错误的)答案 - 见下文
您可以使用这些指令完成重定向(按此顺序):
这将首先重定向所有以
开头的请求index.php
到相应的缩短的 url,然后使用第二条规则默默地提供index.php/etc
。编辑 - 请继续阅读!
事实上,上面的解决方案会生成无限重定向循环,因为 Apache 采取以下操作(假设我们请求
/index.php/abc
) :RewriteCond
匹配/abc
/abc
RewriteCond/abc
匹配第二个RewriteCond
/index .php/abc
。我们又回到了第 1 点,这是一个循环。请注意...
另一种解决方案是将
/abc
重写为/index.php?path=abc
。这是通过这些规则完成的(请在添加这些规则之前删除您的 RedirectMatch 类似规则):我不知道 CodeIgniter 脚本的内部结构,但与大多数 MVC 脚本一样,它将读取
$_REQUEST['PATH_INFO' ]
了解请求的是哪个页面。您可以稍微修改识别页面的代码,如下所示(我假设页面路径存储在$page
var 中):这不会破坏以前的代码并完成您所要求的操作。
First (and wrong) answer - see below
You can accomplish a redirection with these directives (in this order):
That will first redirect all the requests that begin with
index.php
to the corresponding shortened url, then silently serveindex.php/etc
with the second rule.EDIT - Please read on!
In fact, the solution above generates an infinite redirection loop, because Apache takes the following actions (let's say we request
/index.php/abc
):RewriteCond
matches/abc
/abc
fails firstRewriteCond
/abc
matches secondRewriteCond
/index.php/abc
. We are again at point 1, that's a loop.Please note...
An alternative solution can be to rewrite e.g.
/abc
, to/index.php?path=abc
. That is done by these rules (please, delete your RedirectMatch similar rule before adding these):I don't know the internals of CodeIgniter's scripts, but as most of the MVC scripts, it will read
$_REQUEST['PATH_INFO']
to understand which page is requested. You could slightly modify the code that recognizes the page like this (I assumed that the page path is stored in the$page
var):This won't break the previous code and accomplish what you asked for.