In this article we are going to learn how to set preferred domain (www or non-www) using middleware in laravel.
If you own a website google see’s website as two websites with www and the one with non www and doing this, may lead the content duplication issue while google is indexing your website.
Now a days google is also showing the website as unsecured if they are not using SSL
. So i suggest you to quickly move your site to https
. This tutorial will also help you to move your website to https, but that can be obtained by doing some tweaks inside .htaccess
too.
If you want more information you can always visit google documentation to read more about how to structure your website.
Let’s get started
What are we going to do ?
- Create new file inside app/config called
domain.php
and set preferred domain protocol. - Create new middleware called preferred domain and add it inside
kernel.php
in middleware array. - Create new class
Domain.php
inside App directory and write some function to redirect user to user http or https based on preference.
Step 1 : Create New file Inside App/Config
as domain.php
and set preferred domain protocol
In this step we are just simply going to create one domain.php
file inside app/config
and make it return an array of http and https.
1 2 3 4 5 6 |
<?php return [ 'preferred' => '//', 'protocol' => 'https://', ]; |
Step 2 : Creating middleware called PreferredDomain.php
and Registering it to Kernel.php
Create new middleware by going to the terminal and type php artisan make:middleware PreferredDomain
.
Open that middleware and inside public function handle and just above the return $next($request);
paste
1 2 3 |
if($prefDomain->diff()) { return redirect()->to($prefDomain->translated(), 301); } |
Now, you middleware class should look like this,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
<?php namespace App\Http\Middleware; use Closure; use App\Domain; class PreferredDomain { /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed */ public function handle($request, Closure $next) { $prefDomain = new Domain($request); if ($prefDomain->diff()) { return redirect()->to($prefDomain->translated(), 301); } return $next($request); } } |
After that, register this middleware inside the kernel.php
inside the protected $middleware [];
1 2 3 4 5 6 7 8 |
protected $middleware = [ \App\Http\Middleware\CheckForMaintenanceMode::class, \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class, \App\Http\Middleware\TrimStrings::class, \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class, \App\Http\Middleware\TrustProxies::class, \App\Http\Middleware\PreferredDomain::class, ]; |
Step 3 : Create new Domain.php to Configure Redirect
Create new class inside App
directory as Domain.php
and add these code below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
namespace App; use Illuminate\Http\Request; class Domain { const REDIRECT_HTTP = 'http://'; const REDIRECT_HTTPS = 'https://'; const REDIRECT_WWW = '//www.'; const REDIRECT_NOWWW = '//'; const REGEX_FILTER_WWW = '/(\/\/www\.|\/\/)/'; const REGEX_FILTER_HTTPS = '/^(http:\/\/|https:\/\/)/'; /** * @var \Illuminate\Http\Request */ protected $request; /** * @var string */ protected $translated; /** * @param \Illuminate\Http\Request $request */ public function __construct(Request $request) { $this->request = $request; } /** * @return bool */ public function isEqual(): bool { return $this->request->fullUrl() !== $this->translated(); } public function diff(): bool { $this->translated = $this->translate(); return $this->isEqual(); } public function translated(): string { if (!$this->translated) { $this->translated = $this->translate(); } return $this->translated; } public function translate(): string { $protocol = $this->getHttpProtocol(); $filtered = preg_replace(self::REGEX_FILTER_HTTPS, $protocol, $this->request->fullUrl()); return preg_replace(self::REGEX_FILTER_WWW, $this->getDomainPreference(), $filtered); } public function getHttpProtocol(): string { if (!$this->request->secure()) { return self::REDIRECT_HTTP; } return config('domain.protocol') ?? self::REDIRECT_HTTP; } public function getDomainPreference(): string { return config('domain.preferred') ?? self::REDIRECT_WWW; // If you want your site to be redirected to non www just use REDIRECT_NOWWW } } |
This is it, if you try to redirect to https://websitename.com
then would be automatically redirect to https://www.websitename.com
.
If you have any problem. Make sure to mention them via comments.
Leave a Reply