重写地址时使用.htaccess强制ssl

重写地址时使用.htaccess强制ssl,.htaccess,.htaccess,我目前正在使用这个.htaccess将对带有目录的页面的所有请求重定向到我的index.php RewriteEngine on RewriteCond $1 !^(index\.php|cas) RewriteRule ^(.*)$ /seattle/index.php/$1 [L] 这很好,生成了隐藏index.php的URL,我在index.php中有代码,可以使URL看起来干净 但现在我需要强制页面通过ssl连接,所以我尝试了 RewriteEngine on RewriteCond

我目前正在使用这个.htaccess将对带有目录的页面的所有请求重定向到我的index.php

RewriteEngine on
RewriteCond $1 !^(index\.php|cas)
RewriteRule ^(.*)$ /seattle/index.php/$1 [L]
这很好,生成了隐藏index.php的URL,我在index.php中有代码,可以使URL看起来干净

但现在我需要强制页面通过ssl连接,所以我尝试了

RewriteEngine on
RewriteCond %{SERVER_PORT} 80
RewriteCond $1 !^(index\.php|cas)
RewriteRule ^(.*)$ https://example.com/seattle/index.php/$1 [L]
它可以强制ssl,但现在它还强制url包含index.php:

https://example.com/seattle/index.php/pagename
而不是我想要的

https://example.com/seattle/pagename
我缺少什么?

要更改协议(HTTP->HTTPS)和/或域名(
www.example.com
->
example.com
),需要正确的重定向(“301永久重定向”或“302找到/Temp重定向”)

因此,您不能将重写和重定向结合起来,仍然显示原始URL。必须有两个不同的规则,并且应首先列出用于更改协议/域的规则。例如:

RewriteEngine on

# force HTTPS for all URLs
RewriteCond %{HTTPS} =off
RewriteRule . https://example.com%{REQUEST_URI} [R=301,L]

# other rewrite rules
RewriteCond $1 !^(index\.php|cas)
RewriteRule ^(.*)$ /seattle/index.php/$1 [L]
我添加的规则将所有HTTP URL重定向到HTTPS。如果只需要重定向其中的一部分,请通过附加的
RewriteCond
行添加适当的条件

%{HTTPS}
是检查SSL是否打开或关闭的最常见和最“合适”的方法(但这取决于您的具体情况和服务器配置)。在检查
%{HTTPS}
时,当您的站点运行在非标准端口(而不是80)上时,您是安全的。您可以改用
%{SERVER\u PORT}=80
(适用于大多数情况)

根据以上规则,重写
http://example.com/seattle/pagename
将分两步执行:

  • 301重定向到
    https://example.com/seattle/pagename
  • 重写(内部重定向)到
    /settle/index.php/pagename