将 URL 重写为 index.php 但避免在 URL 中使用 index.php [英] Rewrite URL to index.php but avoid index.php in the URL

查看:29
本文介绍了将 URL 重写为 index.php 但避免在 URL 中使用 index.php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

所以像 http://host/test 这样的 URL 应该由 index.php 和像 http://host/index.php/test 应该重定向到 http://host/test 然后由 index.php 处理(无需将浏览器重定向到 index.php)

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]

推荐答案

需要查看请求行中的 URL 来查看是否已经请求了 /index.php/...:>

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]

这篇关于将 URL 重写为 index.php 但避免在 URL 中使用 index.php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆