PHPackages                             mohdraquib/laravel-secure-middleware - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. mohdraquib/laravel-secure-middleware

ActiveLibrary

mohdraquib/laravel-secure-middleware
====================================

Laravel middleware to enforce HTTPS, HSTS, and WWW/Non-WWW redirection.

v1.0.0(9mo ago)05MITPHPPHP ^8.1

Since Jul 12Pushed 5mo agoCompare

[ Source](https://github.com/mohammadraquib/laravel-secure-middleware)[ Packagist](https://packagist.org/packages/mohdraquib/laravel-secure-middleware)[ RSS](/packages/mohdraquib-laravel-secure-middleware/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependenciesVersions (9)Used By (0)

Laravel Secure Middleware
=========================

[](#laravel-secure-middleware)

A Laravel middleware package that enforces secure web behavior like automatic HTTPS redirection, forcing or removing `www.`, and setting the HSTS header for stricter browser security. Built for simplicity, flexibility, and modern Laravel projects.

✨ Features
----------

[](#-features)

- 🔐 Automatically redirects all HTTP traffic to HTTPS (`AlwaysUseHTTPS`)
- 🌐 Forces all URLs to remove the `www.` prefix (`ForceNonWWW`)
- 🌐 Or forces all URLs to use the `www.` prefix (`ForceWWW`)
- 🔒 Adds HTTP Strict Transport Security headers (`EnableHSTS`)
- 📦 Easy to install via Composer
- 🚀 Works out-of-the-box with Laravel's middleware stack

📦 Installation
--------------

[](#-installation)

Install the package using Composer:

```
composer require mohdraquib/laravel-secure-middleware
```

🧩 Usage
-------

[](#-usage)

Register the middleware in your Laravel application's `app/Http/Kernel.php`.

### ➕ Add to Global Middleware Stack

[](#-add-to-global-middleware-stack)

```
use MohdRaquib\SecureMiddleware\AlwaysUseHTTPS;
use MohdRaquib\SecureMiddleware\EnableHSTS;
use MohdRaquib\SecureMiddleware\ForceNonWWW;
// or use ForceWWW instead of ForceNonWWW

protected $middleware = [
    // ...
    AlwaysUseHTTPS::class,
    EnableHSTS::class,
    ForceNonWWW::class, // or ForceWWW::class
];
```

### ➕ Or Register as Route Middleware

[](#-or-register-as-route-middleware)

```
protected $routeMiddleware = [
    'https.redirect' => \MohdRaquib\SecureMiddleware\AlwaysUseHTTPS::class,
    'hsts' => \MohdRaquib\SecureMiddleware\EnableHSTS::class,
    'remove.www' => \MohdRaquib\SecureMiddleware\ForceNonWWW::class,
    'force.www' => \MohdRaquib\SecureMiddleware\ForceWWW::class,
];
```

Then apply to specific routes:

```
Route::get('/secure', function () {
    return 'Secure Route';
})->middleware(['https.redirect', 'hsts', 'remove.www']);
```

🧱 Middleware Details
--------------------

[](#-middleware-details)

### `AlwaysUseHTTPS`

[](#alwaysusehttps)

Redirects all HTTP requests to their HTTPS equivalents. Prevents unsecured traffic automatically.

### `EnableHSTS`

[](#enablehsts)

Adds the `Strict-Transport-Security` header to all secure (HTTPS) responses to instruct browsers to always use HTTPS.

### `ForceNonWWW`

[](#forcenonwww)

Redirects all `www.example.com` URLs to `example.com`, maintaining SEO consistency and simplifying domain access.

### `ForceWWW`

[](#forcewww)

Redirects all `example.com` URLs to `www.example.com`, if you prefer using the `www.` subdomain.

🔄 Example Redirects
-------------------

[](#-example-redirects)

- `http://www.example.com/test` → `https://example.com/test`
- `https://example.com/test` → `https://www.example.com/test` (if using `ForceWWW`)

📄 License
---------

[](#-license)

This package is open-sourced software licensed under the [MIT license](LICENSE).

👤 Author
--------

[](#-author)

**Mohammad Raquib**
[GitHub](https://github.com/mohammadraquib)

---

Secure your Laravel application in seconds with smart middleware!

###  Health Score

33

—

LowBetter than 75% of packages

Maintenance65

Regular maintenance activity

Popularity4

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity50

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~2 days

Total

8

Last Release

290d ago

Major Versions

v0.0.7 → v1.0.02025-07-27

### Community

Maintainers

![](https://www.gravatar.com/avatar/b97a0540c335d066ee6936feb388d3b02b189eda5937bdc62d8e527b9663c4e7?d=identicon)[mohammadraquib](/maintainers/mohammadraquib)

---

Top Contributors

[![mohammadraquib](https://avatars.githubusercontent.com/u/17101064?v=4)](https://github.com/mohammadraquib "mohammadraquib (11 commits)")

---

Tags

laravelhstsforce wwwsecure middlewarealways use httpsforce non www

### Embed Badge

![Health badge](/badges/mohdraquib-laravel-secure-middleware/health.svg)

```
[![Health](https://phpackages.com/badges/mohdraquib-laravel-secure-middleware/health.svg)](https://phpackages.com/packages/mohdraquib-laravel-secure-middleware)
```

###  Alternatives

[bepsvpt/secure-headers

Add security related headers to HTTP response. The package includes Service Providers for easy Laravel integration.

5484.7M9](/packages/bepsvpt-secure-headers)[rtconner/laravel-likeable

Trait for Laravel Eloquent models to allow easy implementation of a 'like' or 'favorite' or 'remember' feature.

394388.0k5](/packages/rtconner-laravel-likeable)[hemp/presenter

Easy Model Presenters in Laravel

247592.6k1](/packages/hemp-presenter)[slowlyo/owl-admin

基于 laravel、amis 开发的后台框架~

61214.2k26](/packages/slowlyo-owl-admin)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
