2012-03-10 125 views
0

好的。我遇到了這個問題,我試圖刪除文件url中的最後一個斜槓,例如http://domain.com/styles/styles.css/。我得到了加斜槓到最後的代碼,但不知道如何去做條件。mod_rewrite目錄的尾部斜槓和文件的URL刪除

如果URL有extesion然後取出年底削減 其他添加斜線..

在這裏我得到了什麼,現在一些博客說,它的解決方案,但仍不能正常工作了我的期望。

RewriteCond %{REQUEST_URI} !\.[^./]+$ 
RewriteCond %{REQUEST_URI} !(.*)/$ 
RewriteRule ^(.*)([^/])$ http://%{HTTP_HOST}/$1$2/ [L,R=301] 

也是一個問題,當我輸入http://domain.com/index它去http://domain.com/inde/。 需要你的幫助的人..提前感謝。

+0

順便說一句,爲什麼會被任何參照'styles.css中/'比網站本身寫的不好HTML等?唯一一次你真的需要修正斜槓是用於像「http:// example.com/coolproject」這樣的根頁面。你爲什麼不修正要求帶有額外斜線的樣式表的HTML?通過允許'style.css /'工作,你可以說把一個文件作爲一個目錄是可以的。 – Kaz 2012-03-10 10:53:55

回答

0

在您的htaccess中添加以下代碼,以便更好地理解。

RewriteCond %{HTTP_HOST} !^\.yourdomain\.com$ [NC] 
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L] 

I have a link : domain.com/folder/ 
it will change to : domain.com//folder 

此外,您可以關閉mod_dir的重定向,方法是將DirectorySlash關閉。

+0

這是如何工作的? – naviciroel 2012-03-10 05:40:38

+0

看到我編輯的答案。 – Milap 2012-03-10 05:45:12

+0

和你一樣的問題http://stackoverflow.com/questions/3917542/remove-trailing-slash-with-mod-rewrite – Milap 2012-03-10 05:54:53

0

爲什麼你想爲這樣的「傢俱」文件做一個外部重定向?內部重定向肯定是你想要的嗎?

Options  -MultiViews 

RewriteCond %{REQUEST_URI} !-d 
RewriteRule ^(.+)/$  $1   [L] 

我建議您關閉mutliviews,如果你不使用它,因爲這可能會產生子請求混淆東西。

0

您的RewriteCond條件在邏輯上反轉,因爲您在那裏有!運算符。所以重寫只適用於那些沒有擴展名的輸入,並且沒有尾隨斜線!

您可以爲沒有條件用一條規則做到這一點:

# Match any sequence of characters, ending in a dot followed 
# by one or more characters that don't contain dots or slashes, 
# followed by a final trailing slash. 
# 
# Rewrite this absolutely and treat as redirect. 

RewriteRule ^(.*\.[^./]+)/$ /$1 [L, R=301]