我希望每個請求都被轉發到 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]
- 1 回答
- 0 關注
- 144 瀏覽
添加回答
舉報
0/150
提交
取消