vendredi 23 août 2019

non-www to www url redirect using htaccess - Laravel, Heroku

I tried several ways to redirect all the traffic to https://www.example.com when user enters:

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

all of them work, but when I extend the path of the root url, that is to http://example.com/login or /register or /anything

I get 404 error.

These is what happens:

example.com/login or 
www.example.com/login or 
http://example.com/login or        (fail)
http://www.example.com/login or 
https://example.com/login or       (fail)
https://www.example.com/login

2 of them fails to redirect to https://www.example.com

these is my .htaccess file

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews -Indexes
    </IfModule>

    RewriteEngine On
    RewriteCond %{HTTPS} !=on
    RewriteCond %{HTTP:X-Forwarded-Proto} !https
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L] 

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

I have added below to my AppServiceProviders class:

public function boot()
{
    if (App::environment('production')) 
    {
        URL::forceScheme('https');
    }
}

I tried lot of different methods but all of them fail (even using middleware).



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire