0

I'm wanting to re-direct all my web traffic to https://example.com

So-far I've got everything except https:www.example.com re-directing fine

This is what my Virtual Host Config file looks like

<VirtualHost _default_:80>
  DocumentRoot "/opt/bitnami/apache2/htdocs"
  RewriteEngine On
  RewriteCond %{HTTPS} !=on
  RewriteRule ^/(.*) https://example.com/ [R,L]
  RewriteCond %{HTTPS_HOST} ^https://www.example.com [NC]
  RewriteRule ^/(.*) https://example.com/ [R,L]
  <Directory "/opt/bitnami/apache2/htdocs">
  ........

The only site that doesn't redirect is

https://www.example.com

What am I missing??

2 Answers2

1

HTTPS_HOST doesn't exist as far as I'm aware, you want HTTP_HOST. Additionally, the HTTP_HOST variable does not include the scheme.

Also, although technically it will still work, you might want to escape your .'s (except in RewriteRule substitutions) as those have a special meaning in a regular expression context (meaning "any character"). Of course a dot also qualifies as any character, it's still good practice.

<VirtualHost _default_:80>
  DocumentRoot "/opt/bitnami/apache2/htdocs"
  RewriteEngine On
  RewriteCond %{HTTPS} !=on
  RewriteRule ^/(.*) https://example.com/ [R,L]
  RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
  RewriteRule ^/(.*) https://example.com/ [R,L]
  <Directory "/opt/bitnami/apache2/htdocs">
Thomas
  • 131
  • 3
0

As Thomas mentioned, you want to use HTTP_HOST. Also your info is redundant and even not useful. Also you redirect port 80 to 443, which means HTTPS is going to be off (not set) anyway. And to redirect on port 443, you need that redirect in the 443 definition.

# http://... to https://...
<VirtualHost _default_:80>
  DocumentRoot "/opt/bitnami/apache2/htdocs"
  RewriteEngine On
  RewriteRule ^/(.*) https://example.com/$1 [R,L]
  ...
</VirtualHost>

# https://www.example.com -> https://example.com
<VirtualHost _default_:443>
  DocumentRoot "/opt/bitnami/apache2/htdocs"
  RewriteEngine On
  RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
  RewriteRule ^/(.*) https://example.com/$1 [R,L]
  ...
</VirtualHost>

Also you probably want the $1 so you do not lose the path when redirecting.

As mentioned by Thomas, \. is a good idea or you could end up matching more than expected (for a host, it's unlikely, though...)

Alexis Wilke
  • 2,210
  • 1
  • 20
  • 37