htaccess 重写规则删除子目录和 .php 扩展名

htaccess rewrite rule remove subdirectory and .php extension

我需要重写我网站的 url 并使其隐藏子文件夹和 .php 扩展名,如下所示:

原文:http://192.168.100.5/project/controller/report.php?unit=1&year=2021

目标:http://192.168.100.5/project/report?unit=1&year=2021

我当前的规则不起作用

Options -Indexes
RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f

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

如何实现?谢谢

您将需要 2 个 .htaccess 个文件。

站点根目录.htaccess:

RewriteEngine On

# forward everything to controller/ if not a file or directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* controller/[=10=] [L]

controller/.htaccess:

RewriteEngine On

# add .php extension internally
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ .php [L]