将URL重写为index.php,但避免URL中包含index.php

发布于 2024-09-03 15:59:53 字数 546 浏览 2 评论 0原文

我试图在内部将所有请求重定向到index.php,并使用 .htaccess 文件在外部重定向所有包含index.php 的请求。

因此像 http://host/test 这样的 URL 应该由 index.php 处理,而像 http://host/index.php/test 应重定向到 http://host/test 然后由index.php处理(不将浏览器重定向到index.php)

我尝试了以下操作,但总是收到一条消息“重定向太多.. ”:

RewriteRule ^index\.php/?(.*)$ /$1 [R,L]
RewriteRule .* index.php/$0 [L]

I'm trying to internally redirect all requests to index.php and externally redirect all requests that contain index.php using a .htaccess file.

So URLs like http://host/test should be processed by index.php and URLs like http://host/index.php/test should be redirected to http://host/test and then processed by index.php (without redirecting the browser to index.php)

I tried the following but always get a message "Too many redirects...":

RewriteRule ^index\.php/?(.*)$ /$1 [R,L]
RewriteRule .* index.php/$0 [L]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

丘比特射中我 2024-09-10 15:59:53

您需要查看请求行中的 URL 以查看是否已请求 /index.php/...

RewriteCond %{THE_REQUEST} ^GET\ /index\.php/?([^ ]*)
RewriteRule ^index\.php/?(.*) /$1 [R,L]
RewriteCond $0 !^index\.php($|/)
RewriteRule .* index.php/$0 [L]

You need to look at the URL in the request line to see if /index.php/… has been requested:

RewriteCond %{THE_REQUEST} ^GET\ /index\.php/?([^ ]*)
RewriteRule ^index\.php/?(.*) /$1 [R,L]
RewriteCond $0 !^index\.php($|/)
RewriteRule .* index.php/$0 [L]
深巷少女 2024-09-10 15:59:53

除此之外,如果您想在不重定向浏览器的情况下执行此操作,那么您不想使用 [R] 选项,这意味着 R 重定向浏览器。

试试这个:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule ^(index.php/)?.* index.php [L]
</IfModule>

Among other things, if you want to do it without redirecting the browser then you don't want to use the [R] option, which means Redirect the browser.

Try this:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule ^(index.php/)?.* index.php [L]
</IfModule>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文