Laravel 5 - redirect to HTTPS

Cover Image for Laravel 5 - redirect to HTTPS
Matheus Mello
Matheus Mello
published a few days ago. updated a few hours ago

Laravel 5 - How to Redirect to HTTPS πŸš€

So, you're working on your first Laravel 5 project and you're not sure how to redirect your app to HTTPS? No worries, I've got you covered! In this blog post, I'll show you how to easily redirect your app to HTTPS using Laravel 5.

The Challenge 🧐

The challenge here is that you have multiple domains pointing to your app, but only two out of three are using SSL. You want to handle the redirect to HTTPS within your app's logic rather than using the .htaccess file. Let's get started!

Solution - Middleware to the Rescue! πŸ¦Έβ€β™‚οΈ

In Laravel 5, you can use Middleware to handle your HTTPS redirect logic. Here's what you need to do:

  1. First, open your terminal or command prompt and navigate to your Laravel project's directory.

  2. Next, create a new Middleware file by running the following command:

php artisan make:middleware ForceHttps
  1. Now, open the newly created ForceHttps.php file located in the app/Http/Middleware directory.

  2. Inside the handle method of the Middleware file, add the following code:

public function handle($request, Closure $next)
{
    if (!$request->secure() && !app()->environment('local')) {
        return redirect()->secure($request->getRequestUri());
    }

    return $next($request);
}

Let me explain what this code does:

  • First, it checks if the request is not secure using the $request->secure() method.

  • Then, it checks if the app's environment is not "local" using the app()->environment('local') method. This ensures that the redirect only happens in non-local environments (e.g., production, staging).

  • If both conditions are met, it redirects the request to the secure HTTPS version using redirect()->secure($request->getRequestUri()).

  • Finally, if the conditions are not met, it continues processing the request by calling $next($request).

  1. Now that you have your Middleware ready, you need to register it in the Laravel framework. Open the app/Http/Kernel.php file.

  2. Find the $middleware array and add the following line to it:

protected $middleware = [
    // Other Middleware...
    \App\Http\Middleware\ForceHttps::class,
];
  1. That's it! Your app is now set up to redirect to HTTPS using Middleware. Give it a try by accessing your app using HTTP, and it should automatically redirect you to the secure HTTPS version.

🌟 Bonus Tip: Handling Cloudflare with Page Rules

If you're using Cloudflare for your domain, you can easily handle your HTTPS redirect using Page Rules in the Cloudflare control panel. Here's how to do it:

  1. Log in to your Cloudflare account and navigate to your domain's control panel.

  2. Go to the "Page Rules" section and click on "Create Page Rule".

  3. Set the "URL pattern" to http://*yourdomain.com/* (replace yourdomain.com with your actual domain name).

  4. Under "Settings", select "Always Use HTTPS" from the dropdown.

  5. Save the Page Rule.

And that's it! Cloudflare will take care of redirecting all HTTP requests to HTTPS for you.

Conclusion πŸŽ‰

Redirecting your Laravel 5 app to HTTPS is now a piece of cake! By using Middleware, you have full control over your app's logic to handle the redirect and ensure a secure browsing experience for your users. And if you're using Cloudflare, don't forget to set up the Page Rule for easy HTTPS handling.

Now it's your turn! Give it a try and let me know in the comments how it worked for you. And if you have any questions or need further assistance, feel free to reach out.

Happy coding! πŸ‘¨β€πŸ’»


More Stories

Cover Image for How can I echo a newline in a batch file?

How can I echo a newline in a batch file?

updated a few hours ago
batch-filenewlinewindows

πŸ”₯ πŸ’» πŸ†’ Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Matheus Mello
Matheus Mello
Cover Image for How do I run Redis on Windows?

How do I run Redis on Windows?

updated a few hours ago
rediswindows

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! πŸš€ Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Matheus Mello
Matheus Mello
Cover Image for Best way to strip punctuation from a string

Best way to strip punctuation from a string

updated a few hours ago
punctuationpythonstring

# The Art of Stripping Punctuation: Simplifying Your Strings πŸ’₯βœ‚οΈ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Matheus Mello
Matheus Mello
Cover Image for Purge or recreate a Ruby on Rails database

Purge or recreate a Ruby on Rails database

updated a few hours ago
rakeruby-on-railsruby-on-rails-3

# Purge or Recreate a Ruby on Rails Database: A Simple Guide πŸš€ So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? πŸ€” Well, my

Matheus Mello
Matheus Mello