Our entire site is to be served over https. I have 'https' in each route. However, how do I redirect them to https if they attempt it over http?
Route::group(array('https'), function()
{
// all of our routes
}
Our entire site is to be served over https. I have 'https' in each route. However, how do I redirect them to https if they attempt it over http?
Route::group(array('https'), function()
{
// all of our routes
}
If you want to redirect to the same URL but using https, you should use
Request::getRequestUri()
instead ofRequest::path()
:For laravel 5.1 you should use given code in
App\Http\Providers\RouteServiceProvider@boot
Now you can use this in routes file.
you can also add
['before' => 'force.ssl']
in$router->group()
inI've had a problem with forcing ssl while doing POST request. It would always redirect to GET. This happens because
Redirect::secure()
is by default using a 302 redirect.To make sure your POST request are redirected properly, use something like
This will make sure your request will keep original request method after redirect occurs.
For users using Laravel 4/5 and Elastic Beanstalk, forcing HTTPS is difficult using these methods because the
isSecure()
will returnfalse
. Further, using.htaccess
redirects will result in a redirect loop for Chrome and delayed page load times in Firefox.This set up is for
aws
on a Windows machine. Linux may vary slightly?After hours of my own attempts, I managed to get all HTTP requests forwarded to HTTPS using the following steps:
Obtain an SSL certificate. Guides and providers are numerous and can be found via a Google search.
Upload the certificate to AWS using the
aws
console command. The command structure is:Create an Elastic Beanstalk application. Proceed through the setup process. Once the application is setup, go to Configuration -> Network Tier -> Load Balancing and click the gear icon.
Select Secure listener port as 443. Select Protocol as HTTPS. Select the
CERTIFICATE_NAME
from step 2 for SSL certificate ID. Save the configuration.Go to your Console. Click EC2 Instances. Click Load Balancers. Click through the load balancers. Click Instances and scroll down to see the EC2 instances assigned to that load balancer. If the EC2 instance has the same name as your Application URL (or something close), take note of the DNS Name for the load balancer. It should be in the format
awseb-e-...
Go back to your Console. Click CloudFront. Click Create Distribution. Select a Web distribution.
Set up the distribution. Set your Origin Domain Name to the load balancer DNS name you found in step 5. Set the Viewer Protocol Policy to Redirect HTTP to HTTPS. Set Forward Query Strings to Yes. Set Alternate Domain Names (CNAMEs) to the URL(s) you want to use for your application. Set SSL Certificate to the
CERTIFICATE_NAME
you uploaded in step 2. Create your distribution.Click on your distribution name in CloudFront. Click Origins, select your origin, and click Edit. Ensure your Origin Protocol Policy is Match Viewer. Go back. Click Behaviors, select your origin, and click Edit. Change Forward Headers to Whitelist and add Host. Save.
Go to your Console. Click Route 53. Click Hosted Zones. Click Create Hosted Zone. Set up your domain name. Once set up, click Create Record Set. Enter your A record. Select Alias as Yes. Your Alias Target is your CloudFront distribution. Save the record.
Set up your nameservers for your domain to point to the Route 53 nameservers. Wait for everything to propagate, which could be a few hours. Go to your URL. You will be automatically redirected to HTTPS.
"But wait, my links don't go to HTTPS!?" You need to handle the
X-Forwarded-Proto
header that CloudFront will pass. For Laravel 4, follow this guide. For Laravel 5, run this:And then add this to the
EB_SSL_Trust
file:And add this to your
App\Http\Kernel.php
file:Note: All your assets, such as CSS, JS or images, need to be sent over HTTPS. If you use Laravel to create these links, use
secure_asset()
to create the HTTPS URL in your View.Combining previous answers to use constants and methods that are available in Laravel 4.2.
routes.php
filters.php
The use of filters has been deprecated in Laravel 5.1.*. This is a perfect job for a MiddleWare.
Create a Middleware and in the handle section put
Then simply register your middleware in your Kernel.php and use it with your routes or controllers.