2013-06-01 37 views
1

我一直在試圖強制HTTPS以及刪除php擴展,但當我將它們混合使用時,我總是在瀏覽器上獲得重定向循環。強制HTTPS以及刪除文件擴展名

我搜索了各地的Stackoverflow,但只有一個關於具體的事情,而不是兩個。

這是我目前有:

RewriteEngine on 
RewriteCond %{SERVER_PORT} 80 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php 

RewriteCond %{HTTPS} off 
RewriteRule . https://%{HTTP_HOST}%{REQUEST_URI}$1 [R=301,L] 
+0

爲什麼你有'的RewriteCond%{SERVER_PORT} 80'?這意味着該規則將僅在其非HTTPS;是一個錯字? –

+0

@BurhanKhalid我不好,那部分是一個錯字。感謝您指出。 – abellao

回答

2

這裏就是我用它來解決這個問題;

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

ErrorDocument 404 http://mysite.co.uk/404page/404.html 

#Force from http to https 
RewriteCond %{SERVER_PORT} 80 
RewriteCond %{HTTP_HOST} !^mysite.co.uk$ 
RewriteRule ^(.*)$ https://mysite.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] 
+0

完美!非常感謝,它完美無瑕。 – abellao

+0

很高興幫助;) – Switchfire

1

@Switchfire - 工作重定向!非常感謝。但如果你在網址中有散列(#),則會出現一些錯誤。

所以我做了一些改變。並且還做出 1.避免直接訪問目錄 2.重定向既WWW或非www請求https://www

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

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

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php