php - 如何编写 .htaccess 将所有请求转发到 index.php 的任何子文件夹中
问题描述
我希望将每个请求转发到 index.php,但要尊重请求是在哪个子文件夹中发出的。我可以让它们都转到根 index.php,但不能转到特定子文件夹中的 index.php。
示例:
site.com/testurl > site.com/index.php
site.com/admin/testurl > site.com/admin/index.php
site.com/randomname/randomname > site.com/randomname/index.php
我不想对任何子文件夹进行硬编码以使其保持动态。
目前,我将此作为我的 .htaccess 文件:
DirectoryIndex index.php
# enable apache rewrite engine
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ 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 server
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Push every request to index.php
RewriteRule ^(.*)$ index.php [QSA]
额外的功劳(不确定 .htaccess 是否可能):让它知道 index.php 是否在子目录中,如果没有,则回退到根 index.php
解决方案
# 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]
推荐阅读
- css - 角材料表标题文本断开到下一行
- c# - 无法将“System.DBNull”类型的对象转换为 C# 中的“System.String”类型
- php - 模态中的输入值总是得到最后一条记录
- google-apps-script - 如何在范围上使用 CalendarApp.getCalendarById
- vba - VBA、PowerPoint 冻结
- http - 从 http 获取特定字段到我们的 zabbix 服务器
- php - 如何在表单模型绑定 laravel 中调用第二个模型?
- protractor - Allure JS protractor-cucumber-framework cucumberOpts - 不止一个格式化程序
- swift - Swift - 在初始化程序中调用函数并在外部定义它
- angular - 阻止用户或管理员使用 rest api 的最佳实践