0

I'm having an issue with apache. I would like to redirect:
http://www.example.com to https://www.example.com - ok
http://example.com to https://www.example.com - ok
https://example.com to https://www.example.com - not redirected

This is the setting of my vhost.conf in Apache:

<NameVirtualHost *:80>

<VirtualHost *:80>
    ServerAdmin user@example.com
    ServerName example.com
    ServerAlias www.example.com
    Redirect permanent / https://www.example.com/
    DocumentRoot /home/example/public_html
    ErrorLog /home/example/logs/error.log
    CustomLog /home/example/logs/access.log combined
</VirtualHost>

<VirtualHost *:443>
    ServerAdmin user@example.com
    ServerName example.com
    ServerAlias www.example.com
    RewriteEngine On
    RewriteCond %{HTTPS} off [OR]
    RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
    RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
    DocumentRoot /home/example/public_html
    ErrorLog /home/example/logs/error.log
    CustomLog /home/example/logs/access.log combined
</VirtualHost>

Can anyone help me with this issue?

0

I use this:

RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ %{REQUEST_SCHEME}://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

"RewriteCond %{HTTPS} off" has no effect since it is already on port 443

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.