dang
dang

Reputation: 2412

htaccess redirection issue for www and https

I have domain - example.com. I am using Laravel 5.

I want always to redirect to https://www.example.com/beta

Right now, my htaccess is:

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>
    RewriteEngine On
    RewriteBase /beta/

    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    RewriteCond %{HTTPS} !=on
    RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301]

    # Redirect Trailing Slashes...
    RewriteRule ^(.*)/$ /$1 [L,R=301]

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

Right now, for example.com/beta - it redirects to https://example.com/beta

but when we go to www.example.com/beta - it redirects to http://www.example.com/https://www.www.example.com/beta

Please advise.

Upvotes: 2

Views: 49

Answers (1)

anubhava
anubhava

Reputation: 785246

Have it this way:

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>
    RewriteEngine On
    RewriteBase /beta/

    RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
    RewriteCond %{HTTPS} !=on
    RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
    RewriteRule ^(.*)$ https://www.%1%{REQUEST_URI} [L,R=301,NE]

    # Redirect Trailing Slashes...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ /$1 [L,R=301,NE]

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

Make sure to clear browser cache before testing this change.

Upvotes: 2

Related Questions