2012-10-18 98 views
0

即使用戶輸入它,我也不希望index.php出現在我的URL中。這可能嗎?即使用戶請求,使用.htaccess從url中刪除index.php

這是幾次嘗試後的變化。我已經接近幾次了,但現在它已經到了。

RewriteEngine On 
RewriteBase /sub-dir/ 
RewriteCond %{REQUEST_URI} index\.php$ //If URL ends in index.php 
RewriteRule (.*)index\.php $1 //Somehow remove index.php from the url 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule . index.php [L] 

目前我已永久設置,如果用戶輸入其中domain.com/sub-dir/my-perma-lin/它基於我-固定鏈接頁面上的繩子,看起來像我Perma Link。我想要的是,如果用戶提交以index.php結尾的任何URL,它只會從URL中刪除它,但保留其他所有內容。

domain.com/sub-dir/index.php - > domain.com/sub-dir/
domain.com/sub-dir/my-perma-link/index.php - >域。 com/sub-dir/my-perma-link

我已經在http://htaccess.madewithlove.be/中寫了很多規則,可以很好地工作,但是當我將它上傳到Dreamhost時,什麼都不起作用。

例如,這要根據測試儀

RewriteEngine On 
RewriteBase /sub-dir/ 
RewriteCond %{REQUEST_URI} \.php //Not needed but thought it would/should help 
RewriteRule (.*)(index\.php)+ $1 [L,R=301,NC] 

工作,但它只是刪除一切後/分DIR/

我既失去了一些東西超明顯的或者是不可能的.. 。

回答

1

你需要一些標誌添加到您的規則:

RewriteEngine On 
RewriteBase /sub-dir/ 
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)index\.php(\?|\) 
RewriteRule^/%1 [L,R=301] 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule . index.php [L] 

您可以消除RewriteCond %{REQUEST_URI} index\.php$條件,因爲正在由RewriteRule中的正則表達式檢查該條件。您需要在正則表達式的末尾包含$,幷包含標記L以停止重寫,並且將R=301重定向。

+0

感謝您的建議,但是這會從url中移除所有內容。 domain.com/sub-dir/pema-link也只是變成了domain.com/sub-dir/。這就是爲什麼我原來處於這種狀況,儘管這似乎並沒有阻止現在的行爲。 –

+0

@VianEsterhuizen我明白你的意思了,我已經改變了答案以匹配請求本身,所以它不會匹配由最後一條規則在內部重寫的'index.php'。 –

+0

非常感謝!這是完美的。如果你有時間,我會很感激,如果你能解釋第三行。你爲什麼要找A-Z的3-9個字符?爲什麼有三件事到那一行? –