Laravel 5 - redirect to HTTPS
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:
First, open your terminal or command prompt and navigate to your Laravel project's directory.
Next, create a new Middleware file by running the following command:
php artisan make:middleware ForceHttps
Now, open the newly created
ForceHttps.php
file located in theapp/Http/Middleware
directory.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)
.
Now that you have your Middleware ready, you need to register it in the Laravel framework. Open the
app/Http/Kernel.php
file.Find the
$middleware
array and add the following line to it:
protected $middleware = [
// Other Middleware...
\App\Http\Middleware\ForceHttps::class,
];
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:
Log in to your Cloudflare account and navigate to your domain's control panel.
Go to the "Page Rules" section and click on "Create Page Rule".
Set the "URL pattern" to
http://*yourdomain.com/*
(replaceyourdomain.com
with your actual domain name).Under "Settings", select "Always Use HTTPS" from the dropdown.
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! π¨βπ»