[英].htaccess Redirect any file in directory to index.php of same directory
[英].htaccess redirect if root, index.php, not file or not directory
目前我有一個處理請求的控制器(使用友好的URL)。 要重定向到此控制器,請使用以下.htaccess:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ __controller.php?p=$1 [L,QSA]
然后我有一個index.php重定向到/ home這是主頁(我強制這樣,所以主頁也使用控制器):
<?php
header('Location: home', true, 301);
exit();
?>
但是,我還希望它發送到這個控制器,如果文件是index.php或者它是根域(因為我不再希望主頁是www.test.com/home而只是www.test.com ),所以以下每個都將通過控制器:
www.test.com/friendly-url/123/post-topic(目前有效)
但是也:
www.test.com/index.php和www.test.com
顯然,由於index.php文件存在,最后2個條件目前不起作用,因此.htaccess不會通過控制器發送它。
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^index.php$ /home [L,R=301,QSA]
RewriteRule ^$ __controller.php?p=home [L,QSA]
RewriteRule ^(.*)$ __controller.php?p=$1 [L,QSA]
你在找這個嗎?
那你就不需要PHP重定向了
用以下內容替換.htaccess代碼:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (?!^index\.php/?$)^(.+)$ __controller.php?p=$1 [L,QSA,NC]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.