htaccess 帮助,从 URL 中删除字符串
我想知道是否可以从 URL 中删除 index.php?基本上在网站的某些页面上我有这样的结构,
http://www.domain.com /index.php/members/register,但其他页面我有这样的URL结构,http://www.domain.com/category/products/id/5,我想知道 htaccess 是否可以在需要时删除 index.php 和任何属性斜杠?我该怎么做呢?
I am wondering, is it possible to remove index.php from an URL? Basically on some pages in a site I have this structure,
http://www.domain.com/index.php/members/register, but other pages I have URL structures like this, http://www.domain.com/category/products/id/5, I want to know is it possible with htaccess to remove the index.php and any attributed slashes when needed? How would I go about doing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,你可以。使用此规则,任何请求的
/index.php
都将被删除:但是您最好从一开始就使用正确的 URL,以便您的应用程序提供链接不包含
/ 的文档。索引.php
。Yes, you can. With this rule any requested
/index.php
will be removed:But you should better use the proper URLs right from the start so that you application is serving documents whose links don’t contain the
/index.php
.如果你想全局重写index.php/controller/action
这个.htaccess配置应该可以解决问题:
这个配置在Apache上检查文件/目录是否存在于磁盘上(即请求与磁盘上的真实资源匹配),并且如果需要,将请求重写到您的前端控制器。
因此
http://www.domain.com/resources/image.png
应该返回图像资源。并且
http://www.domain.com/user/show/5
应该透明地重写为http://www.domain.com/index.php/user/show/5< /code>
通过此配置,您可以删除应用程序 URL 中的所有 index.php 引用,并将重写工作留给 Web 服务器。
If you want to globally rewrite index.php/controller/action
This .htaccess configuration should do the trick:
This configuration checks on Apache whether the file/directory exists on disk or not (i.e. the request match a real resource on disk), and rewrite the request to your front controller if needed.
So
http://www.domain.com/resources/image.png
should return the image resource.And
http://www.domain.com/user/show/5
should transparently rewrite tohttp://www.domain.com/index.php/user/show/5
With this configuration, you can remove all index.php references in your application URLs and leave the rewriting to the web server.