2015-12-06 49 views
2

我需要一個.htaccess,幫助:htaccess的 - HTTP到HTTPS和.html到/

軍隊http://https://

軍隊.html/

我有什麼事這麼遠:

Options -MultiViews 
RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^([^\.]+)$ $1.html [NC,L] 

回答

1
Options -MultiViews 

RewriteEngine On 
#1 This line checks if the https is off 
RewriteCond %{HTTPS} ^off$ 
#then, redirect to https 
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [NC,L,R] 
#2 this line checks if the request is /file.html 
RewriteCond %{THE_REQUEST} /([^.]+)\.html [NC] 
#then redirect /file.html to /file 
RewriteRule^/%1 [NC,L,R] 

#3 if the request is not for dir 
RewriteCond %{REQUEST_FILENAME} !-d 
#and the request is an existing filename 
RewriteCond %{REQUEST_FILENAME}.html -f 
#then rewrite /file to /file.html 
RewriteRule ^([^/]+)/?$ $1.html [NC,L] 

在上面的例子中,當原方案是所述第一條件被滿足HTTP ,然後處理該規則。 HTTP轉到HTTPS。第一輪重寫處理在此結束。

在第二輪中,mod_rewrite接受URI /file.html並且規則將其重定向到/ file,因爲/ file不存在於目錄中,所以我們需要將它重寫爲原始文件#3。

+1

這工作完美,謝謝! –

1
RewriteEngine On 
RewriteCond %{HTTP_HOST} ^example\.com [NC] 
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [R,L] 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^([^\.]+)$ $1.html [NC,L] 

我以前使用過他們兩個,但我沒有一起使用它們。希望這可以幫助。

(編輯) 要實際FORCE擴展看起來像目錄:

RewriteBase/
RewriteCond %{THE_REQUEST} \s/+(.+?)\.html[\s?] [NC] 
RewriteRule^/%1/ [R=302,L,NE] 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=302,NE,L] 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{DOCUMENT_ROOT}/$1\.html -f [NC] 
RewriteRule ^(.+?)/?$ $1.html [L] 

http://alexcican.com/post/how-to-remove-php-html-htm-extensions-with-htaccess/ http://www.inmotionhosting.com/support/website/ssl/how-to-force-https-using-the-htaccess-file

+0

這是更好的,可惜/example.html不是逼着/例如 –

+0

通過迫使你想讓它從/anything.html改變他們的網址/或任何東西只是什麼/或anything.html都將工作? – spencdev

+0

我編輯添加我假設你想要的。 – spencdev

1

首先,確保mod-rewrite已啓用。
然後,把你的htaccess的代碼(這應該是在文檔根目錄文件夾)

Options -MultiViews 

RewriteEngine On 
RewriteBase/

# Redirect http urls to https equivalent 
RewriteCond %{HTTPS} off 
RewriteRule ^(.*)$ https://{HTTP_HOST}/$1 [R=301,L] 

# Redirect existing /path/file.html to /path/file/ 
RewriteCond %{REQUEST_FILENAME} -f 
RewriteCond %{THE_REQUEST} \s/(.+?)\.html\s [NC] 
RewriteRule^%1/ [R=301,L] 

# Internally rewrite back /path/file/ to /path/file.html (if existing) 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{DOCUMENT_ROOT}/$1\.html -f 
RewriteRule ^(.+)/$ $1.html [L] 

警告

  1. 確保提供相同的文檔根目錄爲http用於HTTPS(阿帕奇ssl塊配置)
  2. 請注意,這將創建虛擬目錄(通過添加尾隨 斜槓),這可能會弄亂您的html資源(如果您使用的是相對路徑 而不是絕對路徑)。如果是這樣,使用絕對路徑,而不是
相關問題