2016-01-17 134 views
1

/filename.php成爲/文件名 - 失敗刪除尾部斜槓和php/html擴展名。跟蹤斜槓場景無法刪除擴展

/filename.php/成爲/filename.php - 失敗!

/文件名/成爲文件名 - 成功!

我將如何刪除具有結尾斜槓的方案的擴展?

Options +MultiViews 

RewriteEngine On 

# remove trailing slash 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s] 
RewriteRule ^(.+?)/$ /$1 [R=301,L] 

# remove php/html extension 
RewriteCond %{THE_REQUEST} /index\.(php|html)[\s/?] [NC] 
RewriteCond %{REQUEST_URI} !/system/ [NC] 
RewriteRule ^(.*?)index\.(?:php|html)(.*)$ $1$2 [R=301,NE,L] 
+0

我看不出filename.php'如何'重定向到'filename'因爲你的規則檢查'* index。(php | html)*'。你究竟想在這裏完成什麼?請更具體一點,因爲你的問題說了一件事,但你的代碼說另一件事。 –

+0

該文本是我正在努力完成的。代碼是我的嘗試。到目前爲止,我認爲我實際完成的所有工作都是刪除斜線。 –

回答

2

您可以使用這條規則:

# externally redirect /dir/file.php to /dir/file and remove index 
RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index)?(.*?)\.(?:html?|php)/?[\s?] [NC] 
RewriteRule^/%1%2 [R=302,L,NE] 

# remove trailing slash 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s] 
RewriteRule ^(.+?)/$ /$1 [R=302,L] 
+1

美麗。謝謝。 –

0

你會用這個更好:

RewriteEngine On 

# Trim trailing slash (only if request is not for a directory) 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)/$ /$1 [L,R=301] 

# Trim .php/.html from request (exclude requests to /system/*) 
# Ex: /page.php -> /page 
RewriteCond %{REQUEST_URI} !/system/ [NC] 
RewriteRule ^(.+).(?:php|html)$ /$1 [L,R=301] 
相關問題