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 have a problem, where for some reason
Request::secure()
returns false, even when the url ishttps
, it could be because $_SERVER['HTTPS'] value doesn't exist.This is a workaround:
If you have to use Laravel 4 itself to handle the redirecting (like me), I'd go for the following setup (explanation as comments in the code):
Route filter:
Then apply the filter as a before filter to your route or route group. eg:
I don't understand about HTTP and HTTPS in detail, so I'm sorry if this answer isn't very good.
It's my understanding that there is an issue that even when client and (client specified) server are using HTTPS,
Request::secure()
can return false because your application may be running on a different server, which is possibly not receiving a https request.I'm hosting my laravel app in heroku and it seems it does that. My guess is that the primary (client specified) server is a load balancer and when the request is forwarded, it arrives at the other server as a normal HTTP request.
When such forwarding can happen, you should not just check for
Request::secure()
to betrue
. I was instructed (by someone in #laravel @ irc.freenode.com) to also checkRequest::server('HTTP_X_FORWARDED_PROTO')
to see if it's equal to'https'
.So if you intend to follow the other advice in here and perform a redirect in case of non-secure, try checking for this server parameter too.
Using App::before
You might be able to take advantage of the
App::before()
block in theapp/filters.php
file.Change the block to include a simple check to see if the current request is secure, and if not, redirect it.
Using Filters
Another option might be to create a filter like so. People generally store this also in
app/filters.php
.You can then enforce that new filter to any of your routes, route groups, or controllers like this.
Individual Route
Route Group
Controller
You'll need to do this in your controller's
__construct()
method.This worked for me in Apache 2.4
I changed .htaccess in Laravel's root folder
From
<IfModule mod_rewrite.c> RewriteEngine On RewriteRule ^(.*)$ public/$1 [L] </IfModule>
To
<IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{HTTPS} off RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] RewriteRule ^(.*)$ public/$1 [L] </IfModule>
Another answer might be to let your web server handle this. If you are using Apache, you can use the RedirectSSL feature to make sure all requests are going to the HTTPS version of your site, and if not redirect them. This will happen before Laravel even get's the request.
Apache RedirectSSL
If you're on NGINX, you can accomplish this by having two server blocks. One for normal HTTPS on port 80, and another for HTTPS on port 443. Then configure the normal server block to always redirect to ssl version.
I'd personally go with this option as PHP itself doesn't have to process anything. It's generally cheaper to process a check like this at the web server level.