为了账号安全,请及时绑定邮箱和手机立即绑定

如何编写 .htaccess 以将所有请求转发到它们发出的任何子文件夹中的 index.php

如何编写 .htaccess 以将所有请求转发到它们发出的任何子文件夹中的 index.php

PHP
红糖糍粑 2023-04-21 10:14:06
我希望每个请求都被转发到 index.php,但尊重请求是在哪个子文件夹中进行的。我可以让它们全部转到根 index.php,但不是特定子文件夹中的 index.php。示例:site.com/testurl > site.com/index.phpsite.com/admin/testurl > site.com/admin/index.phpsite.com/randomname/randomname > site.com/randomname/index.php我不想对任何子文件夹进行硬编码以使其保持动态。目前,我将其作为我的 .htaccess 文件:DirectoryIndex index.php# enable apache rewrite engineRewriteEngine onRewriteCond %{HTTPS} offRewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]RewriteRule ^(.*)$ http://%1%{REQUEST_URI} [R=301,QSA,NC,L]# Deliver the folder or file directly if it exists on the serverRewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-d# Push every request to index.phpRewriteRule ^(.*)$ index.php [QSA]额外功劳(不确定 .htaccess 是否可行):让它知道 index.php 是否在子目录中,如果不在则回退到根目录 index.php
查看完整描述

1 回答

?
智慧大石

TA贡献1946条经验 获得超3个赞

# Determines whether the request doesn't end with index.php:

RewriteCond %{REQUEST_URI} !/index.php$


# Redirect to the requested path, followed by index.php:

RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^(.*)/(.*)$    /$1/index.php [L]


RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_URI} !/index.php$

RewriteRule ^(.*) /index.php [L]


查看完整回答
反对 回复 2023-04-21
  • 1 回答
  • 0 关注
  • 91 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信