htaccess 帮助,从 URL 中删除字符串

发布于 2024-09-27 02:22:46 字数 379 浏览 1 评论 0原文

我想知道是否可以从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

虐人心 2024-10-04 02:22:46

是的,你可以。使用此规则,任何请求的 /index.php 都将被删除:

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /index\.php[/?\ ]
RewriteRule ^index\.php(/(.*))?$ /$2 [L,R=301]

但是您最好从一开始就使用正确的 URL,以便您的应用程序提供链接不包含 / 的文档。索引.php

Yes, you can. With this rule any requested /index.php will be removed:

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /index\.php[/?\ ]
RewriteRule ^index\.php(/(.*))?$ /$2 [L,R=301]

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.

淡忘如思 2024-10-04 02:22:46

如果你想全局重写index.php/controller/action

这个.htaccess配置应该可以解决问题:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule> 

这个配置在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:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule> 

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 to http://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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文