2011-06-21 99 views
0
RewriteEngine on 
RewriteCond %{http_HOST} ^domain.com [NC] 
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301] 

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^([^/]+)/$ $1.php 
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php 

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$ 
RewriteRule (.*)$ /$1/ [R=301,L] 

第一條規則正在工作,因此它正確地重定向瀏覽器。但是,第二條規則應該是剝離.php,以便訪問者可以轉到domain.com/services而不是domain.com/services.php,但不起作用。第三個條件增加了/到最後,起作用。mod_rewrite刪除.php不工作

任何想法,爲什麼剝.PHP給了我一個404錯誤:

The requested URL /s/p/domain.com/public/packing-tips.php was not found on this server? 

有什麼建議?

+0

如果你直接去URL(最後帶.php),它會顯示你的網頁?例如,如果您訪問domain.com/services.php或domain.com/packing-tips.php,那麼您是否會收到404? –

+0

不,我沒有得到404,頁面顯示完全如其應該。但是,當我從地址欄手動刪除.php時,它會顯示上述錯誤。 – brianrhea

回答

0

試試這個親愛的:

RewriteRule ^([^/]+)/$1 

希望會幫助你。

0

請嘗試刪除這條線,並再次檢查:

RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php 
0

對於剝離出PHP擴展使用此代碼的.htaccess:

Options +FollowSymlinks -MultiViews 
RewriteEngine on 

RewriteCond %{THE_REQUEST} ^GET\s.+\.php [NC] 
RewriteRule ^(.+)\.php$ /$1 [NE,R=301,L,NC] 

RewriteCond %{REQUEST_URI} !\.php$ [NC] 
RewriteCond %{REQUEST_FILENAME}.php -f 
RewriteRule ^(.*)/?$ /$1.php [L] 
+0

當我轉到domain.com/sizes-and-pricing.php時,它顯示正確。當我刪除.php時,出現此錯誤: 在此服務器上未找到請求的URL /sizes-and-pricing/.php。 – brianrhea

+0

@samseaborn:稍微修改我的答案,以照顧您遇到的尾部斜槓'/'問題。請再試一次。 – anubhava

0
RewriteEngine on 
# force canonical name 
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC] 
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,QSA] 

# Add .php to requested resource (if such php file does exist) 
# e.g. www.example.com/services/ => www.example.com/services.php 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.+)/$ /$1.php [QSA,L] 

# Force trailing slash to be present (only if such file does not exist) 
# e.g. www.example.com/services => www.example.com/services/ 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*[^/])$ /$1/ [R=301,QSA] 

如果您要求此URL domain.com/services它會如果這樣的文件/文件夾不存在,則將其重定向到domain.com/services/。然後,如果(只有IF)確實存在,則它將被重寫爲。如果沒有這樣的PHP文件,那麼你應該看到404錯誤。

從理論上講,你可以在「force canonical name」規則之後添加這些行,然後你可以稍後擺脫一些RewriteCond規則(如果你有很多重寫規則很有用),但是你必須在你的站點上驗證這一點 - 它適用於我,但你可能有特定的情況:

# Do not do anything for already existing files 
RewriteCond %{REQUEST_FILENAME} -f [OR] 
RewriteCond %{REQUEST_FILENAME} -d 
RewriteRule .+ - [L]