Apache redirection of both http to https and non w

2019-02-26 06:43发布

问题:

I'm having an issue with apache2. I would like to redirect both port 80 to port 443 but also redirect non www to www.

In the following config I have the http to https redirection working fine:

<VirtualHost *:80>
    Redirect permanent / https://www.example.com/
</VirtualHost>

<VirtualHost *:443>
    ServerName www.example.com
    DirectoryIndex index.html
    ...
</VirtualHost>

The problem is this I can redirect http to https fine, however when I go to https://example.com it does not redirect to https://www.example.com

Can anyone help me with this issue?

回答1:

In your <VirtualHost *:443> section, add the following rules:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L]