简体   繁体   English

如何在.htaccess上设置仅HTTPS标头?

[英]How to set HTTPS-only header on .htaccess?

I'm trying to set HTTPS-only header with .htaccess. 我正在尝试使用.htaccess设置仅HTTPS标头。

This works on MAMP Pro, but on the live server the header is missing on 301 redirect responses: 这适用于MAMP Pro,但在实时服务器上,301重定向响应中的标头缺失:

Header always set Strict-Transport-Security '...' env=HTTPS

The issue can be fixed with this: 可以通过以下方法解决此问题:

Header always set Strict-Transport-Security '...' "expr=%{HTTPS} == 'on'"

But it doesn't work with MAMP: 500 Internal Server Error is returned. 但是它不适用于MAMP:返回500 Internal Server Error

Can I make expressions (like that "expr=%{HTTPS} == 'on'" ) work on MAMP? 我可以使表达式 (例如"expr=%{HTTPS} == 'on'" )在MAMP "expr=%{HTTPS} == 'on'"工作吗? How? 怎么样?

In my experience, MAMP/MAMP Pro does not necessarily work with .htaccess. 以我的经验,MAMP / MAMP Pro不一定与.htaccess一起使用。 Mainly due to the fact that MAMP makes your machine a local server, so depending on your .htaccess some rules may be conflicting with MAMP's setup. 主要是由于MAMP使您的计算机成为本地服务器,因此根据您的.htaccess,某些规则可能与MAMP的设置冲突。 Every time I run a local instance of one of my sites on MAMP I have to temporarily remove any and all .htaccess files. 每次我在MAMP上运行一个站点的本地实例时,都必须临时删除任何和所有.htaccess文件。

In terms of getting your server to force ssl connections, I used these lines of code at the top of .htaccess below "Options -Indexes": 为了使您的服务器强制ssl连接,我在.htaccess顶部“选项-索引”下面使用了以下代码行:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM