Redirect all traffic to www https

My application is running Apache hosted on Ubuntu and I want the following redirects to work:

1> http://example.com       -> https://www.example.com
2> http://www.example.com   -> https://www.example.com
3> https://example.com      -> https://www.example.com

      

I am using the following lines in the default apache config file:

RewriteEngine on
RewriteCond %{HTTP_HOST}   !^$
RewriteRule ^/?(.*)         https://www.example.com/$1 [L,R]

      

So the first two redirects work fine, but the third doesn't. Is the third possible? if so, how?

Thank.

+3


source to share


2 answers


I have a site where I want all traffic entering the virtual host to go to port 443. I configured the virtual host definition to forward ports 80 like this:

RewriteEngine on
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^/?(.*)         https://www.example.com/$1 [L,R]

      

This will redirect any traffic entering port 80 to 443.



Make sure you have configured the Server Name and ServerAlias ​​correctly. In this example, you have something like:

<VirtualHost *:80>
    ServerName www.example.com
    ServerAlias *.example.* example.* example.com

    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteCond %{REQUEST_METHOD} ^TRACE
        RewriteRule .* - [F]
        RewriteCond %{REQUEST_METHOD} ^TRACK
        RewriteRule .* - [F]

        #redirect all port 80 traffic to 443
        RewriteCond %{SERVER_PORT} !^443$
        RewriteRule ^/?(.*) https://www.example.com/$1 [L,R]
    </IfModule>

</VirtualHost>

      

You will then also configure the definition of virtual host 443 (excluding the port rewrite section).

+4


source


I know it has been a while, but I think you might be looking for something like this:

RewriteCond %{HTTPS_HOST}   !^$
RewriteRule ^/?(.*)         https://www.mysite.com/$1 [L,R]

      



The key part is S

in%{HTTPS_HOST}

You should be able to post this right after what you already have.

0


source







All Articles