【问题标题】:Redirect to https and www in single redirect in Apache在 Apache 的单一重定向中重定向到 https 和 www
【发布时间】:2016-12-18 11:17:24
【问题描述】:

我想重定向以下域

http://example.com
http://www.example.com
https://example.com

https://www.example.com

在单个 http 请求中

我知道它可以在两个请求中使用

#First rewrite any request to the wrong domain to use the correct one (here www.)
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

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

但是额外的重定向会增加页面的加载时间。

我想在一个 http 请求中做这两件事

我在 ubuntu 上使用 apache2

提前致谢

【问题讨论】:

    标签: apache .htaccess http redirect https


    【解决方案1】:

    要在单个 http 请求中重定向到 https 和 www,您可以使用

    RewriteEngine on
    RewriteCond %{HTTPS} off [OR]
    RewriteCond %{HTTP_HOST} !^www
    RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$
    RewriteRule ^ https://www.%1%{REQUEST_URI} [NE,L,R]
    

    【讨论】:

    • 完美运行...必须将这些行添加到安全和正常的虚拟主机配置中
    • @amit-verma 如果网站在 https://example.com 上运行,我将如何做同样的事情?
    • @bugnumber 在这篇文章中查看我的回答stackoverflow.com/questions/1478173/…
    猜你喜欢
    • 2018-11-16
    • 2017-02-02
    • 1970-01-01
    • 1970-01-01
    • 2012-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-22
    相关资源
    最近更新 更多