Skip to main content

Command Palette

Search for a command to run...

Laravel 12 Force Redirect HTTP To HTTPS

How To Force Redirect HTTP To HTTPS In Laravel 12

Updated
2 min read
Laravel 12 Force Redirect HTTP To HTTPS
I

Experienced Web Developer | Laravel | PHP | jQuery | HTML | CSS I’m a skilled web developer with extensive experience building dynamic and responsive web applications using Laravel, PHP, jQuery, HTML, and CSS. I specialize in creating clean, scalable, and maintainable code, with a focus on performance and user experience. Whether it's developing RESTful APIs, building custom CMS features, or crafting responsive front-ends, I bring a problem-solving mindset and a passion for quality to every project.

In modern web applications, security is always a top concern. This article, “Laravel 12 Force Redirect HTTP to HTTPS”, explains why switching from HTTP to HTTPS is important for data protection, user confidence, and SEO benefits.

If your Laravel 12 project is still loading through http://, it’s strongly recommended to redirect all traffic to https://. In this tutorial, we’ll explore multiple approaches to achieve HTTP-to-HTTPS redirection in Laravel 12. You’ll discover methods such as:

  • Server-level redirection (Apache, Nginx)

  • Laravel-based enforcement (AppServiceProvider, custom middleware)

Prerequisites

Before setting up redirection, ensure the following:

  • Your server has a valid SSL/TLS certificate installed

  • You have access to your Laravel project and server configuration (Apache/Nginx)

  • You understand which environment you’re working in (local, staging, production)

Methods to Force Redirect HTTP to HTTPS in Laravel 12

1. Using .htaccess (Apache Server)

For projects hosted on Apache, you can enforce HTTPS by modifying the .htaccess file located inside the public/ directory.

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{HTTPS} !=on
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>

.htaccess

<IfModule mod_rewrite.c>

    <IfModule mod_negotiation.c>

        Options -MultiViews -Indexes

    </IfModule>



    RewriteEngine On



    RewriteCond %{HTTPS} !=on

    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]



    # 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]



    # Send Requests To Front Controller...

    RewriteCond %{REQUEST_FILENAME} !-d

    RewriteCond %{REQUEST_FILENAME} !-f

    RewriteRule ^ index.php [L]

</IfModule>

This will redirect all HTTP requests to HTTPS with a permanent (301) redirect.

Check out full tutorial