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