2017-03-04 50 views
0

首先 - 再次提出這個問題抱歉。它似乎被答覆了一百萬次 - 但我仍然無法讓我的.htaccess工作。通過子目錄中的.htaccess刪除擴展名爲.html的文件。

問題:的public_html我們有一個工作網站和一個.htaccess文件。在一個子目錄被稱爲「分段」我們有另一個網站在public_html中完全獨立於網站。我如何使用.htaccess來處理子目錄以刪除.html擴展名?我們在一個帶有cPanel的共享主機(Hostgator)的apache服務器上。

的.htaccess中的public_html

# -- concrete5 urls start -- 
<IfModule mod_rewrite.c> 
RewriteEngine On 
RewriteBase/

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

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME}/index.html !-f 
RewriteCond %{REQUEST_FILENAME}/index.php !-f 
RewriteRule . index.php [L] 


</IfModule> 
# -- concrete5 urls end -- 

<IfModule mod_headers.c>  
Header set Access-Control-Allow-Origin * 
</IfModule> 
#Header set Access-Control-Allow-Origin * 

的.htaccess分期

RewriteEngine On 
RewriteBase/
RewriteCond %{SCRIPT_FILENAME} !-f 
RewriteCond %{SCRIPT_FILENAME} !-d 
RewriteRule ^(.*)$ $1.html [NC,L] 

任何(詳細)幫助或指導將不勝感激!

+0

爲什麼在'/ staging'文件夾裏的動態配置文件裏有'RewriteBase /'? – arkascha

+0

你已經解釋了你的設置,但是有什麼問題? 「staging」目錄是否由不同的域提供服務? –

+0

是不是與特定目錄相關的「基礎」?即使我使用上面顯示的代碼而沒有「RewriteBase /」行 - 它也不起作用。 – Alex

回答

1

只要保持staging/.htaccess,但除去RewriteBase /,因爲這會導致$1.html/$1.html,而不是/staging/$1.html搜索相關替代路徑。

RewriteBase指令指定用於替換相對路徑的每個目錄(htaccess)RewriteRule指令的URL前綴。

你也不需要NC|nocase,因爲該模式不包含任何「英文字母」的字母。

RewriteCond %{SCRIPT_FILENAME} !-f 
RewriteCond %{SCRIPT_FILENAME} !-d 
RewriteRule ^(.*)$ $1.html [L] 
+0

感謝您的代碼編輯。不幸的是,.html擴展名仍然存在。我不知道發生了什麼事... – Alex

+0

這是什麼意思?當你輸入'http:// www.example.com/staging/some/path/test'時,是否顯示相應的'.../test.html'? –

+0

謝謝奧拉夫。當我直接輸入url而沒有.html擴展名時,頁面現在可以正確加載。所以我認爲你的代碼建議確實可行!我的猜測是,我需要手動從頁面內的鏈接中刪除.html,以使URL不顯示.html – Alex

0

看來你可以使用這個

RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)\.html$ /$1 [L,R=301] 
在子目錄

或修改父htaccess的。 正如我在http://htaccess.madewithlove.be/測試它的工作。

您的頁面是否是真正的靜態html頁面或html地址是由某個CMS的鏈接系統生成的?

+0

所有頁面僅爲靜態頁面 - 不附加CMS。 – Alex

+0

你可以嘗試這些解決方案: http://stackoverflow.com/questions/1992183/how-to-hide-the-html-extension-with-apache-mod-rewrite – labris

相關問題