Set your Preferred Domain on Laravel Application

In this blog post, I would like to show how to set preferred domain from your application context.

Before diving into the code, let's understand why the preferred domain is much important for SEO, web pages indexing on Search Engine.

Follow the link to read in detail from Google support article.

The preferred domain is the one that you would like used to index your website's pages (it is sometimes referred to as the canonical domain). Links may point to your site using both the www and non-www versions of the URL (for instance, http://www.example.com and http://example.com). The preferred domain is the version that you want to be used for your site in the search results.

 

Here, in this guide, I'm going to implement non-www preferred domain for every incoming HTTP request to the application. I have created a custom Laravel Middleware to set the preferred domain.

People usually do this with server configuration, (eg: with .htaccess for apache), but you can also force it from application context. I have used this mechanism with several Laravel applications I have built.

Let's start with creating a middleware with an artisan command.

php artisan make:middleware PreferredDomain

Open up your app/Http/Middleware/PreferredDomin.php and paste code below.

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Redirect;

/**
 * Class PreferredDomain
 * @package App\Http\Middleware
 */
class PreferredDomain
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if (starts_with($request->header('host'), 'www.')) {
            $host = str_replace('www.', '', $request->header('host'));
            $request->headers->set('host', $host);

            return Redirect::to($request->fullUrl(), 301);
        }

        return $next($request);
    }
}

We want to run this middle over every HTTP request that comes to our application, simply we need to register the middleware class in the $middleware property in app/Http/Kernel.php class.

    protected $middleware = [
        ...

        \App\Http\Middleware\PreferredDomain::class,
    ];

Note: If you're still using v4.2 version of Laravel don't worry I have solution for you too as well ;)

 

Open up your app/filters.php

App::before(function ($request) {
    if (starts_with($request->header('host'), 'www.')) {
        $host = str_replace('www.', '', $request->header('host'));
        $request->headers->set('host', $host);

        return Redirect::to($request->fullUrl(), 301);
    }
});


Conclusion

This tutorial heavily depends on Laravel framework, I primarily use Laravel to build my projects.

Thanks for reading up to the end. If you have any feedback please leave your comments below. Feel free to share with friends if you like it.

Last Updated: 21st Jan, 2018

Happy Coding!