2013-06-01 40 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