2016-04-20 62 views
1

「太多重定向」我試圖強制我的根域的子文件夾(/ bbb /)始終顯示爲https。我的.htaccess文件也負責頁面的擴展。.htaccess - 試圖強制https:

我已將.htaccess文件放入我的/bbb/文件夾中,但當我嘗試強制連接到https時,我得到「太多重定向」,沒有它,一切正常。

我的代碼有什麼問題?

Options +FollowSymLinks -MultiViews 
Options +Indexes 
AcceptPathInfo Off 
RewriteEngine on 
RewriteBase /

ErrorDocument 404 https://example.co.uk/404page/404.html 

#Force from http to https 
RewriteCond %{SERVER_PORT} 80 
RewriteCond %{HTTP_HOST} !^bbb.example.co.uk/$ 
RewriteRule ^(.*)$ https://bbb.example.co.uk/$1 [R=301] 

#take off index.html 
RewriteCond %{HTTP_HOST} !^www\. [NC] 
RewriteCond %{REQUEST_URI} ^(.*/)index\.html$ [NC] 
RewriteRule . http://www.%{HTTP_HOST}%1 [R=301,NE,L] 

## hide .php extension 
# To externally redirect /dir/foo.php to /dir/foo 
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC] 
RewriteRule^%1 [R,L,NC] 

## To internally redirect /dir/foo to /dir/foo.php 
RewriteCond %{REQUEST_FILENAME}.php -f 
RewriteRule^%{REQUEST_URI}.php [L]  

## hide .html extension 
# To externally redirect /dir/foo.html to /dir/foo 
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.html [NC] 
RewriteRule^%1 [R,L,NC] 

## To internally redirect /dir/foo to /dir/foo.html 
RewriteCond %{REQUEST_FILENAME}.html -f 
RewriteRule^%{REQUEST_URI}.html [L] 
+1

HTTP_HOST包含** **剛剛的主機名。你的'bbb.example.co.uk /'永遠不會匹配,因爲'/'不是主機名的有效部分。 –

回答

2

問題是在這樣的規則:

#Force from http to https 
RewriteCond %{SERVER_PORT} 80 
RewriteCond %{HTTP_HOST} !^bbb.example.co.uk/$ 
RewriteRule ^(.*)$ https://bbb.example.co.uk/$1 [R=301] 

將此規則更改爲:

#Force from http to https 
RewriteCond %{HTTPS} off 
RewriteCond %{HTTP_HOST} =bbb.example.co.uk 
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301] 

你有狀況逆轉,由於在開始使用!,並有在年底額外的斜槓這將永遠不會匹配,因此使你的條件總是返回true。

確保在測試之前清除瀏覽器緩存。

+0

謝謝,但它仍然說「太多重定向」..我刷新了緩存 – SNos

+0

完全相同的規則在我的Apache上正常工作。你在Apache後面嗎?'%{SERVER_PORT}'在那裏工作正常嗎? – anubhava

+0

我不是用我的服務器,而是服務器提供商one.com – SNos

3

試試這個:

RewriteEngine On 
RewriteCond %{ENV:HTTPS} !on 
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]