2012-04-01 149 views
2

我一直在試圖讓我的客戶端的服務器上爲我正在開發的網站工作,但我根本無法讓它工作。基本上我試圖刪除.html擴展名,並添加一個結尾的斜槓(在URL欄中)。.htaccess刪除URL擴展名,添加跟蹤斜槓

所以,如果有人進入:

-example.com/home/ -----------去----- example.com/home/

-example .com/home ------------去----- example.com/home/

-example.com/home.html ------去 - ---- example.com/home/

-example.com/home.html/ -----去example.com/home/ -----

-example.com/home/.html -----轉到----- example.com/home/

-example.com/home/.html/ ----轉到 - --- example.com/home/

這是我的.htaccess到目前爲止,它工作完美,並做我想要的一切,除了在末尾添加尾部斜線。

這裏是代碼:

#force www 
RewriteCond %{HTTP_HOST} !^www\. 
RewriteRule ^(.*) http://www.%{HTTP_HOST}/1 [R=301,L] 

# remove .html ONLY if requested directly 
RewriteCond %{THE_REQUEST} (\.html\sHTTP/1) 
RewriteRule ^(.+)\.html /1 [R=301,L,QSA] 

# remove trailing slash ONLY if it is not an existing folder 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)/ /1 [L,R=301] 

# rewrite to FILENAME.html if such file does exist and is not a folder 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.html -f 
RewriteRule ^(.*) /1.html [L,QSA] 

所有的託管服務器上我的文件在FILENAME.html的形式,並且都位於根目錄下。

所以,如果有人能幫助我,我會非常感激。

回答

1

修改.htaccess文件,並插入以下

說明: http://eisabainyo.net/weblog/2007/08/19/removing-file-extension-via-htaccess/

例子:

RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.html -f 
RewriteRule ^([^/]+)/$ $1.html 

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

更新頁面上的鏈接

然後,所有的超鏈接,CSS鏈接,圖像等,將需要更新爲具有絕對URL(http://www.site.com/style.css)或相對並且是杜松子酒../。否則,您將遇到諸如CSS無法加載,鏈接不起作用等問題。

+0

不幸的是,我已經嘗試過這種解決方案,它根本不適用於我。我只是得到一個錯誤404找不到頁面。我在OP中發佈的.htaccess完美地工作,它不會在URL的末尾添加斜槓。 – shootingrubber 2012-04-01 10:31:54

+0

我已經更新了答案。試試這個,它更清楚一點。 – msigman 2012-04-01 10:33:52

+0

這對我也不起作用。它添加了斜槓,但不會加載任何CSS或圖像。最重要的是,當我點擊我網站上的任何鏈接時,只需將它們添加到URL欄中的任何鏈接即可。假設我在example.com/home/上,然後單擊測試,然後轉到example.com/test/,然後轉到example.com/home/test。 – shootingrubber 2012-04-01 10:36:57

相關問題