Skip to content

Step 4 of 10

.htaccess, HTTPS and www for Laravel

If your home page works but every other page gives a 404, this chapter is the fix. The .htaccess file sends every request to Laravel.

The rules

These go in public_html/.htaccess. Put the redirects before Laravel's own rule.

<IfModule mod_rewrite.c>
    RewriteEngine On

    # Force HTTPS
    RewriteCond %{HTTPS} !=on
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

    # Pass the Authorization header to PHP (API tokens)
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # Send everything that is not a real file to Laravel
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

The config generator adds the www rule for your domain, and the .htaccess generator writes redirects for old URLs.

Don't overwrite cPanel's lines

cPanel can add a block that sets your PHP version, marked php -- BEGIN cPanel-generated handler. If you replace the whole file, the site can fall back to another PHP version and show a 500 error or Composer version error. Edit the file and add to it, rather than uploading a new one over it.

After turning on HTTPS

  • Set APP_URL to the https:// address, then clear the config cache.
  • Set SESSION_SECURE_COOKIE=true.
  • Check for pages without styles: the browser blocks http:// files on an https:// page.

If you use Cloudflare or a proxy, Laravel also needs to trust it, or it builds http:// links. In bootstrap/app.php, use $middleware->trustProxies(at: '*'); when the site can only be reached through the proxy.

Frequently asked questions

Why does every page except the home page give a 404?

The .htaccess file is missing or not being read. Check that public_html/.htaccess exists; File Manager hides dotfiles until you turn on "Show Hidden Files" in Settings.

Does this work on LiteSpeed?

Yes. LiteSpeed reads Apache .htaccess rewrite rules.

Get new articles in your inbox

Practical tutorials and deep dives, about once a week. No spam, unsubscribe anytime.

Privacy policy

Add DEV Talk to your Home Screen

  1. 1 Tap the Share button in Safari's toolbar.
  2. 2 Scroll down and tap Add to Home Screen.
  3. 3 Tap Add. Open it from the Home Screen to read like an app and to turn on notifications with the bell.

We use cookies to improve your experience and analyze traffic. By continuing, you agree to our Privacy Policy.