Laravel Redirecting Www To Non-www URLs

In the world of web development, ensuring consistency in your website's URLs is crucial for both user experience and search engine optimization (SEO). One common practice is to choose either the www version or the non-www version of your domain and redirect all traffic to that preferred version. In this blog post, we'll explore four different methods to redirect www to non-www URLs in Laravel.

Method 1: Laravel Redirect www to non-www URLs using htaccess

One of the simplest ways to achieve this redirection is by using the .htaccess file. Here's how you can set it up:

  • Open the .htaccess file in your Laravel project's public directory.

  • Add the following code snippet to redirect www to non-www URLs

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]


Replace example.com with your actual domain name.

Method 2: Laravel Redirect www to non-www URLs using Apache2

If you're using Apache as your web server, you can also achieve the redirection by configuring Apache virtual host settings.

  • Open your Apache configuration file (usually located at /etc/apache2/sites-available/your-site.conf).

  • Add the following lines within the <VirtualHost> block:

<VirtualHost *:80>
    ServerName www.example.com
    Redirect permanent / http://example.com/
</VirtualHost>


Replace example.com with your actual domain name. Save the file and restart Apache for the changes to take effect.

Method 3: Laravel Redirect www to non-www URLs using Nginx

For those using Nginx as their web server, the redirection can be achieved through Nginx server block configuration.

  • Open your Nginx configuration file (usually located at /etc/nginx/sites-available/your-site).

  • Add the following server block configuration:

server {
    listen 80;
    server_name www.example.com;
    return 301 $scheme://example.com$request_uri;
}


Replace example.com with your actual domain name. Save the file and reload Nginx for the changes to take effect.

Method 4: Laravel Redirect www to non-www URLs using Middleware

Alternatively, you can implement the redirection within your Laravel application using Middleware.


Create a new middleware by running the following artisan command:

php artisan make:middleware RedirectWWWToNonWWW


  • Open the middleware file (RedirectWWWToNonWWW.php) located in the app/Http/Middleware directory.

  • Update the handle method with the following code:

public function handle($request, Closure $next)
{
    if (strpos($request->getHost(), 'www.') === 0) {
        $newUrl = $request->getScheme() . '://' . substr($request->getHost(), 4) . $request->getRequestUri();
        return redirect()->to($newUrl, 301);
    }

    return $next($request);
}


Register the middleware in the $middleware property of the app/Http/Kernel.php file:

protected $middleware = [
    // Other middleware entries...
    \App\Http\Middleware\RedirectWWWToNonWWW::class,
];


With these four methods, you can effectively redirect www to non-www URLs in your Laravel application, ensuring a consistent browsing experience for your users while adhering to SEO best practices. Choose the method that best fits your server environment and preferences, and enjoy a cleaner and more optimized URL structure for your website.