Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

smartinec's avatar

Force Some Routes to HTTPS

How can I force some Spark routes to HTTPS (/settings, /login, /password, /webhook), but make sure all the other routes are HTTP? Can I wrap middleware based on a URL prefix and not mess with the built-in Spark routes? I'd also like any links from HTTPS pgae back to a non-forced URL to be HTTP. Thanks.

0 likes
5 replies
jlrdw's avatar

A site in general, it's best to go all https.

smartinec's avatar
smartinec
OP
Best Answer
Level 1

This worked for my purpose:

<?php

namespace App\Http\Middleware;

use Closure;
use URL;

class HttpsRedirect
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if(!app()->environment('local') && $request->isMethod('get') && !$request->ajax()) {
            if(
                $request->is('path1*') ||
                $request->is('path2*') ||
                $request->is('path3*')
            ) {
                if(!$request->secure()) {
                    return redirect()->secure($request->path());
                }
            }
            else if($request->secure()) {
                URL::forceScheme('http');
                return redirect($request->path());
            }
        }
        return $next($request);
    }
}

Kernel.php

<?php

namespace App\Http;

use Illuminate\Foundation\Http\Kernel as HttpKernel;

class Kernel extends HttpKernel
{
    /**
     * The application's global HTTP middleware stack.
     *
     * These middleware are run during every request to your application.
     *
     * @var array
     */
    protected $middleware = [
        \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
        \App\Http\Middleware\HttpsRedirect::class,
    ];
smartinec's avatar

What is the Apache overhead when running HTTPS?

Thyrosis's avatar

It depends on the amount of connections you make. If you combine your js and css files so that you only end up with a few requests to build up your page, the performance impact is negligible.

Using SSL requires a handshake on connections, which is not required on default HTTP. This is why HTTPS is a little slower than its unsecured counterpart. The fewer connections there are to be shaken about, the lower the performance impact will be.

Please or to participate in this conversation.