1

我一直在尝试强制 HTTPS 以及删除 php 扩展,但是当我将它们混合在一起时,我总是在浏览器上得到一个重定向循环。

我搜索了 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]
4

2 回答 2

3

这是我用来解决这个问题的方法;

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]   
于 2013-06-05T09:20:55.527 回答
1

@Switchfire - 为重定向工作!多谢。但是,如果您在 url 中有哈希 (#),则会出现一些错误。

所以我做了一些改变。并使其成为 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
于 2017-06-23T01:21:40.307 回答