在Apache中单个重定向中重定向到https和www [英] Redirect to https and www in single redirect in Apache

查看:171
本文介绍了在Apache中单个重定向中重定向到https和www的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想重定向以下域名

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

https://www.example.com


$单个http请求中的b $ b

in a single http request

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

I know it can be done in two requests using

#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]

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

but and extra redirect increases loadtime of page.

我想在一个http请求中执行这两项操作

I want to do both things in one http request

我在ubuntu上使用apache2

I am using apache2 on ubuntu

提前致谢

推荐答案

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

To redirect to https and www in a single http request, you can use

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

这篇关于在Apache中单个重定向中重定向到https和www的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆