2015-10-11 79 views
3

我試圖在我的laravel站點上強制https和非www。這裏是我的.htaccess文件:Laravel 5.1強制非WWW和HTTPS不工作

RewriteEngine On 

# Redirect Trailing Slashes If Not A Folder... 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)/$ /$1 [L,R=301] 

# Handle Front Controller... 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule^index.php [L] 

#if the request is not secure 
RewriteCond %{HTTPS} off 
#redirect to the secure version 
RewriteRule (.*) https://%{HTTP_HOST}/$1 [R=301,L] 

#Redirect to non-WWW 
RewriteCond %{HTTP_HOST} ^www.example.com$ 
RewriteRule ^(.*) https://example.com/$1 [QSA,L,R=301] 

以下是當前重定向

工作重定向

example.com     => https://example.com   (GOOD) 
www.example.com    => https://example.com   (GOOD) 
https://www.example.com  => https://example.com   (GOOD) 

直接到正確的URL工作正常

https://example.com   => https://example.com   (GOOD) 
https://example.com/asdf  => https://example.com/asdf  (GOOD) 

不工作重定向

example.com/asdf    => https://example.com/index.php (BAD) 
www.example.com/asdf   => https://example.com/index.php (BAD) 
https://www.example.com/asdf => https://example.com/index.php (BAD) 

當我不在主頁上我想不通爲什麼它重定向到index.php文件。

回答

9

經過2小時尋找解決方案後,結果非常簡單。

所有我需要做的就是改變我的.htaccess文件的順序,以

RewriteEngine On 

#Redirect to non-WWW 
RewriteCond %{HTTP_HOST} ^www.example.com$ 
RewriteRule ^(.*) https://example.com/$1 [QSA,L,R=301] 

#if the request is not secure 
RewriteCond %{HTTPS} off 
#redirect to the secure version 
RewriteRule (.*) https://%{HTTP_HOST}/$1 [R=301,L] 

# Redirect Trailing Slashes If Not A Folder... 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)/$ /$1 [L,R=301] 

# Handle Front Controller... 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule^index.php [L]