I know that there are several similar questions, but after many tests I am not able to get what I want.
First of all, I read that apache can redirect in three ways: redirect, mod_rewrite or with if-else statement (apache 2.4+).
In the past I simply used:
<VirtualHost *:80> ServerName example.com ServerAlias www.example.com # does not redirect Redirect "/" "https://www.example.com/" </VirtualHost> <VirtualHost *:443> ServerName example.com ServerAlias www.example.com # does not redirect </VirtualHost> I read that I can use a more sophisticated syntax:
<VirtualHost *:80> ServerName example.com ServerAlias www.example.com # does not redirect RewriteEngine On RewriteBase / # redirect all RewriteCond %{REQUEST_URI} ^/ RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L] # how to turn off for localhost and 127.0.0.1? Or
<VirtualHost *:80> ServerName example.com ServerAlias www.example.com <If "%{HTTP_HOST} == 'example.com'"> # how to add multiple condition? Redirect permanent / https://myexample.com/ </If> Sometimes is better to avoid rewrite directive. What can I do to achieve something like this?
1. https://example.com -> no redirect 2. http://example.com -> redirect to https://example.com 3. https://www.example.com -> redirect to https://example.com 4. http://www.example.com -> redirect to https://example.com 5. http://127.0.0.1/anyPath -> redirect to https://127.0.0.1/anyPath (or no redirect) 6. http://localhost/anyPath -> redirect to https://localhost/anyPath (or no redirect) As you can see, I am not an expert in apache configuration.
Thank you