Installing SSL (URLs that start with https://) on your PHP project and implementing that in your code can be achieved by updating .htaccess file of your project but after updating that you have to face the problem to change most of the links of your website like css files, images and scripts because wherever they are declared in http format, after changing the .htacces to redirect your domain to HTTPS all those files wouldn’t be accessible but there is a very simple solution to enforce HTTPS in Laravel projects.
Add these lines in your .htacces file of the root directory of your project.
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{www.example.com}%{REQUEST_URI} [L,R=301]
Replace www.example.com with domain of yours and whenever your domain is redirected to your project with http request it will be forwarded to https. The problem you will face is that most of your external css files wouldn’t be accesible to your code so you have to go to master file and change all of your files path to https or in worst case if you doesn’t have a master file or there are more then one master files then you have to go to each file and update all those files.
The problem of enforcing HTTPS in Laravel can be handled in two ways.
One is that you just check environment of your project i.e \App::environment(‘local’) which is APP_ENV variable value in .env.
1st step is to open AppServiceProvider.php file which is located in app/Providers directory and go to boot method and check for environment if it is in production or any other you need i.e
if(\App::environment('production')) {
}
Now use Illuminate\Contracts\Routing\UrlGenerator; in AppServiceProvider file, and bind it to boot method in its parameter.
public function boot(UrlGenerator $url)
{
if(\App::environment('production')) {
}
}
We will be using UrlGenerator contract for redirection. In UrlGenerator class there is a function forceScheme which accepts argument name of schema i.e https or http.
public function boot(UrlGenerator $url)
{
if(\App::environment('production')) {
$url->forceScheme('https');
}
}
And that’s it. All of your project links would be redirected to HTTPS without changing any link in your project manually. On the other you can define a variable for it in .env file i.e ENFORCE_SSL and check if its true then enforce https. This method we discuss below.
The other method is to declare a variable in .env file called ENFORCE_SSL and assign it boolean value so whenever you assign it the value true, all links or URLs of your project will be redirected to https protocol and you don’t have to manually go and change all of your links to https, Laravel will take care of that itself. So to recap,
Sample code given here.
public function boot(UrlGenerator $url)
{
if(env('ENFORCE_SSL', false)) {
$url->forceScheme('https');
}
//remaining code of yours
}
You can change value of ENFORCE_SSL to true anywhere you need to enforce HTTPS in Laravel.
Hope you find this discussion useful. Let us know if you need to hire Laravel developers for your project.
Our support staff is available 24/7 to take support calls and messages from clients.
We involve client in all stages of software development to deliver satisfaction and peace of mind.
We hire developers that are technically strong and discuss the project thoroughly before starting.
If you are not happy with the quality of work or we fail to achieve final technical goal, we’ll not take your money.
We have worked with some clients for over 7 years. Our commitment and dedication is a matter of pride for us.
We care about your investment. We will tell it straight if you are making a bad decision.
NDA implied. No spam. Privacy guaranteed.
Best web, app, eCommerce and custom software development company. We provide services to more than 100 clients world-wide.
Contact The Right Software today to discuss the your next big idea.