2012-09-14 78 views
2

mod_rewrite規則將index.html附加到以正斜槓結尾的任何URL會是什麼?該規則應該保留任何查詢字符串。我無法使用DirectoryIndex指令,因爲index.html文件並非物理上存在於文件系統中,而是由底層網站框架所需。mod_rewrite添加index.html(如果不存在)的規則

一些示例網址和期望的結果如下所示:

http://example.com/     -> http://example.com/index.html 
http://example.com/?a=1    -> http://example.com/index.html?a=1 
http://example.com/foo/    -> http://example.com/foo/index.html 
http://example.com/foo/?b=2   -> http://example.com/foo/index.html?b=2 
http://example.com/foo/index.html  -> http://example.com/foo/index.html 
http://example.com/foo/index.html?c=3 -> http://example.com/foo/index.html?c=3 

回答

6

查詢字符串自動獲得由所附的mod_rewrite除非查詢字符串本身被修改。這應該是你所需要的:

RewriteEngine On 
RewriteRule ^/?$ /index.html [L,R=301] 
RewriteRule ^/?(.*)/$ /$1/index.html [L,R=301] 

這就所以當有人要求任何一個/結束後,將瀏覽器重定向到相同的URL與index.html底。一個空白的URI是一個特例(第一條規則)。如果您不需要重定向瀏覽器,只需從方括號中刪除,R=301即可。

+0

我認爲你可以幫助你:http://stackoverflow.com/questions/37407592/configure-htaccess-file-with-two-sites-to-serve – xybrek