Ad

Redirect All Urls To HTTPS Except One Which Starts With "/api"

- 1 answer

How can I redirect all requests from http to https except requests to api? Url for api starts with /api. I used this config but seems not working.

RewriteCond %{HTTPS} !=on
RewriteCond %{REQUEST_URI} !^api/
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Could you please tell me where I did it wrong? Project is written in Laravel.

Thanks in advance

Ad

Answer

If you need this you can create a middleware named checkHttps and paste the following code

<?php

namespace App\Http\Middleware;

use Closure;

class checkHttps {

    public function handle($request, Closure $next) {

        if(!request()->is('api/*')) {
            if (!$request->secure()) {
                return redirect()->secure($request->getRequestUri());
            }
        }

        return $next($request);
    }

}

And then add your middleware to kernel

 protected $middlewareGroups = [
    'web' => [
        \App\Http\Middleware\checkHttps::class,
    ],
];

hopefully this will solve your problem.

Ad
source: stackoverflow.com
Ad