Skip to content

Commit 6be7a3e

Browse files
committed
Added headers Middleware
1 parent 708b60e commit 6be7a3e

File tree

3 files changed

+28
-12
lines changed

3 files changed

+28
-12
lines changed

app/Http/Kernel.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class Kernel extends HttpKernel
2121
\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
2222
\App\Http\Middleware\TrimStrings::class,
2323
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
24+
\App\Http\Middleware\Headers::class,
2425
];
2526

2627
/**

app/Http/Middleware/Headers.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace App\Http\Middleware;
4+
5+
use Closure;
6+
use Illuminate\Http\Request;
7+
8+
class Headers
9+
{
10+
public function handle(Request $request, Closure $next)
11+
{
12+
// Check if FORCE_HTTPS is set to true
13+
if (env('FORCE_HTTPS') == 'true') {
14+
\URL::forceScheme('https'); // Force HTTPS
15+
header("Content-Security-Policy: upgrade-insecure-requests");
16+
}
17+
18+
// Check if FORCE_ROUTE_HTTPS is set to true
19+
if (env('FORCE_ROUTE_HTTPS') == 'true' && (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] == 'off')) {
20+
$redirect_url = "https://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
21+
header("Location: $redirect_url");
22+
exit();
23+
}
24+
25+
return $next($request);
26+
}
27+
}
Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1 @@
1-
@if(env('FORCE_HTTPS') == 'true')<?php URL::forceScheme('https'); header("Content-Security-Policy: upgrade-insecure-requests"); ?>@endif
21
<html lang="{{ config('app.locale') }}">
3-
4-
{{-- Redirects to https if enabled in the advanced-config --}}
5-
@if(env('FORCE_ROUTE_HTTPS') == 'true')
6-
@php
7-
if (! isset($_SERVER['HTTPS']) or $_SERVER['HTTPS'] == 'off' ) {
8-
$redirect_url = "https://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
9-
header("Location: $redirect_url");
10-
exit();
11-
}
12-
@endphp
13-
@endif

0 commit comments

Comments
 (0)