Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Pida's avatar
Level 7

Enforcing SSL redirects every http request to homepage

I want to redirect every http request to https. http://example.com/test should be redirected to https://example.com/test.

Enforcing SSL is already working, but every http request gets redirected to the same page (https://example.com/index.php).

This is my .htaccess which is located in /public. I tried three different approaches, each of them directly after RewriteEngine Onas well as after # Handle Authorization Header.

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

    RewriteEngine On
    
    # Force SSL: Laravel
    #RewriteCond %{HTTPS} !=on
    #RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    
    # Force SSL: https://stackoverflow.com/questions/44006146
    #RewriteCond %{SERVER_PORT} 80
    #RewriteRule ^(.*)$ https://example.com/ [R,L]
    
    # Force SSL: Recommendation by hosting company
    RewriteCond %{SERVER_PORT}     !^443$
    RewriteRule (.*)  https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]

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

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

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

I'm using Laravel 5.5.

Thanks, Pida

0 likes
1 reply
Pida's avatar
Pida
OP
Best Answer
Level 7

The .htaccess above is working just fine, I probably had some older version of the page in browser cache.

There's more than one way to enforce SSL. Right now, I'm using these lines immediately after RewriteEngine On:

# Enforce SSL
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R=301]

Please or to participate in this conversation.