# How to force HTTPS in Apache2?

**URL:** <https://community.letsencrypt.org/t/how-to-force-https-in-apache2/124762>\
**Category:** Help\
**Created:** [June 4, 2020, 2:11am UTC](https://community.letsencrypt.org/t/how-to-force-https-in-apache2/124762 "2020-06-04T02:11:28Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![LuizC](https://sea3.discourse-cdn.com/letsencrypt/user_avatar/community.letsencrypt.org/luizc/32/40172_2.png) [@LuizC](https://community.letsencrypt.org/u/LuizC)\
**Post date:** [June 4, 2020, 2:11am UTC](https://community.letsencrypt.org/t/how-to-force-https-in-apache2/124762/1 "2020-06-04T02:11:28Z")

</div>

(e.g. [https://crt.sh/?q=example.com](https://crt.sh/?q=example.com))

My domain is: _ **botboutique.com.br** _

My web server is (include version): _ **Apache 2.4.29** _

The operating system my web server runs on is (include version): _ **Ubuntu 18.04 LTS** _

My hosting provider, if applicable, is: _ **Locaweb** _

I can login to a root shell on my machine (yes or no, or I don’t know): _ **yes** _

I’m using a control panel to manage my site (no, or provide the name and version of the control panel): _ **no** _

The version of my client is (e.g. output of `certbot --version` or `certbot-auto --version` if you’re using Certbot): _ **0.31.0** _

I’m trying to force Apache2 to only serve HTTPS in my domain. Here is the .htaccess that I out in the root folder of Apache (/var/www):

RewriteEngine On  
RewriteCond %{HTTP\_HOST} botboutique.com.br [NC]  
RewriteCond %{SERVER\_PORT} 80  
RewriteRule ^(.\*)$ [https://botboutique.com.br/$1](https://botboutique.com.br/%241) [R,L]

(I take this code from [https://www.whynopadlock.com](https://www.whynopadlock.com))

But it didn’t worked, can you guys help me on this matter?

---

<div class="post-metadata">

**Author:** ![\_az](https://avatars.discourse-cdn.com/v4/letter/_/22d042/32.png) [@\_az](https://community.letsencrypt.org/u/_az)\
**Post date:** [June 4, 2020, 2:35am UTC](https://community.letsencrypt.org/t/how-to-force-https-in-apache2/124762/2 "2020-06-04T02:35:14Z")

</div>

Some possibilities:

1. Something in the rules is not matching
2. Your `.htaccess` file is not actually being read because it’s in the wrong location
3. Your `.htaccess` file is not being processed due to options in your main Apache configuration.

For (1), try see if using this more generic approach helps:

```apache
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R,L]

```

For (2), confirm that your `/var/www/` is indeed your webroot. For example, create a test file to access in your browser:

```
echo "Hello World" > /var/www/hello-world.txt

```

For (3), try adding `AllowOverride All` inside this domain’s `<VirtualHost>` in your main Apache configuration, and reload Apache. See if it has any effect.

---

<div class="post-metadata">

**Author:** ![LuizC](https://sea3.discourse-cdn.com/letsencrypt/user_avatar/community.letsencrypt.org/luizc/32/40172_2.png) [@LuizC](https://community.letsencrypt.org/u/LuizC)\
**Post date:** [June 4, 2020, 3:37am UTC](https://community.letsencrypt.org/t/how-to-force-https-in-apache2/124762/3 "2020-06-04T03:37:49Z")

</div>

Thanks for your kind response, @_az.

1. I tried yor code, but no effects. I tried too:  
RewriteEngine On  
RewriteCond %{SERVER\_PORT} 80  
RewriteRule ^(.\*)$ [https://botboutique.com.br/$1](https://botboutique.com.br/%241) [R,L]  
with no results.

2. At first I get confused about on where to put .htaccess file. At last, I realize that I must to put it in /var/www/www.botboutique.com.br/html. No effects either.

3. Changed my apache2.conf:  
"\<VirtualHost \*:80\>  
ServerName www.botboutique.com.br  
Redirect / [https://botboutique.com.br](https://botboutique.com.br)  
ServerAlias botboutique.com.br \*.botboutique.com.br  
DocumentRoot /var/www/www.botboutique.com.br/html  
\<Directory “/var/www/www.botboutique.com.br/html”\>  
Require all granted  
AllowOverride All  
  
"  
And … none.

Everytime I used  
systemctl restart apache2  
to reboot Apache.

Any clues, please?

---

<div class="post-metadata">

**Author:** ![\_az](https://avatars.discourse-cdn.com/v4/letter/_/22d042/32.png) [@\_az](https://community.letsencrypt.org/u/_az)\
**Post date:** [June 4, 2020, 3:49am UTC](https://community.letsencrypt.org/t/how-to-force-https-in-apache2/124762/4 "2020-06-04T03:49:59Z")

</div>

> [@LuizC](#):
>
> Redirect / [https://botboutique.com.br](https://botboutique.com.br)

The fact that this doesn't do anything points to the possibility that your Apache configuration contains duplicate virtual hosts. If that's the case, that may provide the explanation for why none of your configs seem to do anything.

What does this show:

```
apachectl -t -D DUMP_VHOSTS

```

---

<div class="post-metadata">

**Author:** ![LuizC](https://sea3.discourse-cdn.com/letsencrypt/user_avatar/community.letsencrypt.org/luizc/32/40172_2.png) [@LuizC](https://community.letsencrypt.org/u/LuizC)\
**Post date:** [June 4, 2020, 4:00am UTC](https://community.letsencrypt.org/t/how-to-force-https-in-apache2/124762/5 "2020-06-04T04:00:28Z")

</div>

Here it is:

VirtualHost configuration:  
\*:443 www.botboutique.com.br (/etc/apache2/sites-enabled/apache2-le-ssl.conf:2)  
\*:80 is a NameVirtualHost  
default server vps18569.publiccloud.com.br (/etc/apache2/sites-enabled/000-default.conf:1)  
port 80 namevhost vps18569.publiccloud.com.br (/etc/apache2/sites-enabled/000-default.conf:1)  
port 80 namevhost www.botboutique.com.br (/etc/apache2/sites-enabled/apache2-le-ssl.conf:15)  
alias botboutique.com.br  
wild alias \*.botboutique.com.br  
port 80 namevhost www.botboutique.com.br (/etc/apache2/apache2.conf:241)  
alias botboutique.com.br  
wild alias \*.botboutique.com.br

---

<div class="post-metadata">

**Author:** ![\_az](https://avatars.discourse-cdn.com/v4/letter/_/22d042/32.png) [@\_az](https://community.letsencrypt.org/u/_az)\
**Post date:** [June 4, 2020, 4:08am UTC](https://community.letsencrypt.org/t/how-to-force-https-in-apache2/124762/6 "2020-06-04T04:08:55Z")

</div>

Here is the duplication of your port 80 virtualhost for `botboutique.com.br`:

Number 1:

> [@LuizC](#):
>
> port 80 namevhost www.botboutique.com.br (/etc/apache2/sites-enabled/apache2-le-ssl.conf:15)  
> alias botboutique.com.br  
> wild alias \*.botboutique.com.br

Number 2:

> [@LuizC](#):
>
> port 80 namevhost www.botboutique.com.br (/etc/apache2/apache2.conf:241)  
> alias botboutique.com.br  
> wild alias \*.botboutique.com.br

Only one of these virtual hosts actually has any effect - the other one gets ignored by Apache. Ultimately only one of them should exist.

Try making your changes to the "other one" and see whether they take effect.

---

<div class="post-metadata">

**Author:** ![LuizC](https://sea3.discourse-cdn.com/letsencrypt/user_avatar/community.letsencrypt.org/luizc/32/40172_2.png) [@LuizC](https://community.letsencrypt.org/u/LuizC)\
**Post date:** [June 4, 2020, 4:12am UTC](https://community.letsencrypt.org/t/how-to-force-https-in-apache2/124762/7 "2020-06-04T04:12:27Z")

</div>

@_az It’s not clear for me how to avoid this duplication and how to fix it.

What do you mean when you say “to the other one”?

Sorry, I’m pretty noob.

---

<div class="post-metadata">

**Author:** ![\_az](https://avatars.discourse-cdn.com/v4/letter/_/22d042/32.png) [@\_az](https://community.letsencrypt.org/u/_az)\
**Post date:** [June 4, 2020, 4:23am UTC](https://community.letsencrypt.org/t/how-to-force-https-in-apache2/124762/8 "2020-06-04T04:23:39Z")

</div>

> [@LuizC](#):
>
> Changed my apache2.conf:

Try making the same changes to `/etc/apache2/sites-enabled/apache2-le-ssl.conf`. You should find the virtualhost around line 15.

---

<div class="post-metadata">

**Author:** ![LuizC](https://sea3.discourse-cdn.com/letsencrypt/user_avatar/community.letsencrypt.org/luizc/32/40172_2.png) [@LuizC](https://community.letsencrypt.org/u/LuizC)\
**Post date:** [June 4, 2020, 5:07am UTC](https://community.letsencrypt.org/t/how-to-force-https-in-apache2/124762/10 "2020-06-04T05:07:14Z")

</div>

@_az

Despite an strange inconsistency among browsers (Chrome and Edge, ok | Firefox says that I have mixed content and shows me an yellow padlock), seems that your tips take me out of my nightmare 🙂

Tomorow I’ll test it further.

Thank you very, very much for your support!

---

<div class="post-metadata">

**Author:** ![rg305](https://sea3.discourse-cdn.com/letsencrypt/user_avatar/community.letsencrypt.org/rg305/32/91314_2.png) [@rg305](https://community.letsencrypt.org/u/rg305)\
**Post date:** [June 6, 2020, 1:05am UTC](https://community.letsencrypt.org/t/how-to-force-https-in-apache2/124762/11 "2020-06-06T01:05:25Z")

</div>

It looks like you got it figured out!  
The HTTP requests are being redirected to HTTPS (including their paths) 🙂

```auto
curl -Iki http://botboutique.com.br/x/y/z
HTTP/1.1 302 Found
Date: Sat, 06 Jun 2020 01:03:49 GMT
Server: Apache/2.4.29 (Ubuntu)
Location: https://botboutique.com.br/x/y/z
Content-Type: text/html; charset=iso-8859-1

```

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/letsencrypt/original/3X/c/a/ca6c06ea1ea201324bba7048c6841ce60236468d.png) [@system](https://community.letsencrypt.org/u/system)\
**Post date:** [July 6, 2020, 1:05am UTC](https://community.letsencrypt.org/t/how-to-force-https-in-apache2/124762/12 "2020-07-06T01:05:27Z")

</div>

Closed on July 6, 2020, 1:05am UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
