apache - 使用 htaccess 将用户从 http 重定向到 https

标签 apache .htaccess mod-rewrite

我对 htaccess 一点也不熟悉。我需要 Htaccess 代码,它将 http 重定向到 https。我正在使用下面的代码:

RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

但主要问题是,在尝试测试服务器速度时,我收到以下建议:

To speed up page load times for visitors of your site, remove as many landing page
redirections as possible, and make any required redirections cacheable if possible.

http://xxxx.com/ is a non-cacheable redirect to https://xxxx.com/

那么,如何使其可缓存?

最佳答案

http://xxxx.com/ is a non-cacheable redirect to https://xxxx.com/

错误说明了一切。要使其在浏览器中永久可缓存,请使用 R=301:

RewriteEngine On

RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]

关于apache - 使用 htaccess 将用户从 http 重定向到 https,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25797590/

相关文章:

java - 插件 org.apache.maven.plugins :maven-install-plugin:2. 4 或其依赖项之一无法解析

.htaccess - SEO 友好的 URL 正在工作,但主页不再工作

php - 为什么我的 htaccess 代码不起作用?

用于动态 URL 的 Apache Mod_Rewrite Htaccess

sockets - 如何更改nginx proxy_pass中的request_uri?

Apache2 和 HeartBleed SSL 问题

javascript - 无法在 html 站点中通过 Javascript 读取 xml 文件

php - Wordpress 子目录给出 404

apache - SSL 将用户重定向到 apache 上的错误网站

php - 使用.htaccess重写动态子域