0

I'm using Ubuntu 18.04 with Apache.

I have the following in /etc/apache2/sites-available/000-default.conf:

<VirtualHost *:80>
    RewriteEngine On
    RewriteCond %{SERVER_NAME} =www.example.com [OR]
    RewriteCond %{SERVER_NAME} =example.com
    RewriteRule ^ https://www.%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

I want to redirect all to https AND www.

These get all redirected to https://www.example.com:

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

This doesn't get redirected to https://www.example.com:

https://example.com

I also added the code with <VirtualHost *:443> but this doesn't help.

Why doesn't get https://example.com redirected to https://www.example.com?

David
  • 153
  • 1
  • 7
  • "I also added the code with but this doesn't help." That's the part that would be interesting to add in your post. As `https://www.example.com` will be managed by the "443 vhost" section. Do you have a `.htaccess` file in your website's root that has rewrite directive as well? If so, then rewrite directives in your vhost will be ignored if the `AllowOverride` directive allows it. – Dan Oct 26 '20 at 12:21
  • Does this answer your question? [Force https:// and www. with virtual host apache2](https://askubuntu.com/questions/653923/force-https-and-www-with-virtual-host-apache2) – Dan Oct 26 '20 at 12:38
  • @Dan None of them is working, it seems like `` gets ignored. Any idea why? – David Oct 26 '20 at 12:49

1 Answers1

0

Solution found: There's an own file for HTTPS/SSL/433 at /etc/apache2/sites-available/000-default.conf.

After I added the following code in /etc/apache2/sites-available/000-default.conf, everything is working as expected now:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
David
  • 153
  • 1
  • 7
  • 1
    When I do that, I usually do it in .htaccess. The mod_rewrite rules work in there as well. If you're curious, or want to add it to your answer, [here](https://www.elated.com/mod-rewrite-tutorial-for-absolute-beginners/) is a very basic tutorial. In this case, the same syntax you used will work in .htaccess to do the same exact thing. – KGIII Oct 26 '20 at 16:27