這將是一個有點困難與htaccess的,我想你要做到以下幾點:
- 如果有人訪問的目錄不是根(簡單http://domain.com/),將其重定向到以.html結尾的目錄名稱
- 獲取重定向後,將.html 返回內部重寫到該目錄,以便apache可以爲該目錄提供服務。
第一個是簡單的:
# check to make sure the request isn't actually for an html file
RewriteCond %{THE_REQUEST} !^([A-Z]{3,9})\ /(.+)\.html\ HTTP
# check to make sure the request is for a directory that exists
RewriteCond %{REQUEST_FILENAME} -d
# rewrite the directory to
RewriteRule ^(.+)/$ /$1.html [R]
第二部分是棘手
# check to make sure the request IS for an html file
RewriteCond %{THE_REQUEST} ^([A-Z]{3,9})\ /(.+)\.html\ HTTP
# See if the directory exists if you strip off the .html
RewriteCond %{DOCUMENT_ROOT}/%2 -d
# Check for an internal rewrite token that we add
RewriteCond %{QUERY_STRING} !r=n
# if no token, rewrite and add token (so that directories with index.html won't get looped)
RewriteRule ^(.+)\.html /$1/?r=n [L,QSA]
但是,如果你只是有一幫叫做directory.html
文件,directory2.html
,directory3.html
等等,你想要這樣做,當有人輸入http://domain.com/directory2/到他們的地址ba [R,他們的投放directory2.html
的內容,這將是簡單得多:
# check to make sure the request isn't actually for an html file
RewriteCond %{THE_REQUEST} !^([A-Z]{3,9})\ /(.+)\.html\ HTTP
# check to see if the html file exists (need to do this to strip off the trailing /)
RewriteCond %{REQUEST_URI} ^/(.+)/$
RewriteCond %{DOCUMENT_ROOT}/%1.html -f
# rewrite
RewriteRule ^(.+)/$ /$1.html [L]
只需創建一個名爲'directory'文件夾,把'directory.html'到它,文件重命名爲'index.html'。 – Gerben
@Gerben謝謝你,但那不是我想要的。我不想擁有一堆index.html文件。我想爲我的主頁創建一個index.html文件,但是每個目錄的索引都有不同的名稱。如果我找不到其他解決方案,我會使用你的。 – Jakar
那麼'/ directory /'實際上是否存在?或者你在文檔根目錄下有一個'directory.html'文件,並且想讓它看起來像一個目錄? –