2012-04-03 39 views
49

我有以下線在我的.htaccess文件訪問的index.php:製作index.html的默認值,但允許如果鍵入

DirectoryIndex index.html index.php 

每次我去的index.php它需要我索引html的。是否可以同時允許這兩個,但將index.html保留爲訪問www.domain.com的用戶的默認值?

+0

「index.html'和」index.php「都存在,還是隻有一個? – nkorth 2012-04-04 03:14:08

+1

它們都存在。你是說我的.htaccess文件應該允許我訪問這兩個文件嗎? – 2012-04-04 06:22:33

+1

你在'DirectoryIndex'中只有'index.html'嗎?那麼這將是'example.com'的默認值。其他文件應該在'example.com/index.php'處可用。 – bfavaretto 2012-04-10 03:21:19

回答

73

默認情況下,DirectoryIndex的設置爲:

DirectoryIndex index.html index.htm default.htm index.php index.php3 index.phtml index.php5 index.shtml mwindex.phtml 

Apache會尋找每一個上述文件的,爲了和服務發現當訪問者請求只是一個目錄中的第一個。如果Web服務器在當前目錄中找不到與DirectoryIndex指令中的名稱匹配的文件,則會向瀏覽器顯示一個目錄列表,顯示當前目錄中的所有文件。

順序應該是DirectoryIndex index.html index.php //默認的index.html

參考:Here

+1

感謝隊友,今晚我會刪除.htaccess文件,看看這是否解決了我的問題。我認爲這似乎有點奇怪,因爲在此之前它從未發生過我的任何網站。 – 2012-04-10 04:58:48

+1

非常歡迎:-) – 2012-04-10 05:01:03

1
RewriteEngine on 
RewriteRule ^(.*)\.html$ $1.php%{QUERY_STRING} [L] 

將這兩行放在.htaccess文件的頂部。它會在您的.php頁面的URL中顯示.html。

RewriteEngine on 
RewriteRule ^(.*)\.php$ $1.html%{QUERY_STRING} [L] 

用這個在你的.html頁面的URL中顯示.php。

+0

這看起來很漂亮,但並不完全是我害怕後的情況。 – 2012-04-10 23:12:18

11

如果你使用WordPress,現在有一個過濾器鉤來解決此問題:

remove_filter('template_redirect', 'redirect_canonical'); 

(在將這個你的主題functions.php

這告訴WordPress的不定向index.php回根頁面,但要坐在它的位置。這樣,可以將index.html分配爲.htaccess中的默認頁面,並且可以與index.php一起使用。

+0

這太好了,謝謝你的更新 – 2015-06-17 23:12:20

+0

這是一個救星! – ASR 2016-11-18 06:25:40

1

的DirectoryIndex index.html的index.htm的Default.htm的的index.php index.php3 index.phtml index.php5 index.shtml mwindex.phtml

它不具有任何手段? 你可能只需要添加像這樣!

<IfModule dir_module> 
    DirectoryIndex index.php index.html index.htm 
</IfModule> 

enter image description here

3

我@ TheAlpha同意接受條款的答案,阿帕奇從左至右讀DirectoryIndex的目標文件,如果第一個文件存在,apche的服務它,如果它不那麼接下來的文件送達作爲目錄的索引。所以,如果你有以下指令:

DirectoryIndex file1.html file2.html 

Apache將成爲/file.html爲指標,您需要更改文件的順序,如果你想設置/文件2。HTML作爲指數

DirectoryIndex file2.html file1.html 

您也可以使用上面

RewriteEngine on 

RewriteRule ^$ /index.html [L] 

重寫規則重寫規則一將改寫你的主頁,/index.html重寫設置索引文件內部會發生這樣http://example.com/會告訴你的內容ofindex。 html。

相關問題