Redirect www to non-www URLs in Laravel - TechvBlogs

Redirect www to non-www URLs in Laravel

In this article, You will learn How to Redirect www to non-www URLs in Laravel.


Smit Pipaliya - Author - TechvBlogs
Smit Pipaliya
 

3 weeks ago

TechvBlogs - Google News

In the competitive realm of online presence, ensuring your website appears in search results as you desire is crucial. Many website owners, like techvblogs.com, prefer their site to display without the www prefix. Achieving this involves strategic handling to prevent the loss of valuable visitors. Let's explore effective methods to remove www from your domain and optimize your URL structure.

Leveraging Server-Side Solutions

Nginx Configuration for Seamless Redirection

Nginx, a powerful web server, offers a server-side solution to redirect www to non-www effortlessly. Create a new configuration file, such as /etc/nginx/conf.d/redirect.conf, with the following content:

server {
    server_name www.techvblogs.com;
    return 301 $scheme://techvblogs.com$request_uri;
}

This configuration ensures a smooth redirection from www.techvblogs.com to techvblogs.com, enhancing your website's consistency.

Utilizing .htaccess in Laravel Applications

For those employing Laravel, the .htaccess file in the public folder plays a pivotal role. Add the following code to effectuate the redirection:

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

Ensure mod_rewrite is installed on your server, and activate RewriteEngine to enable this redirection.

Laravel Middleware Approach

Leverage Laravel's flexibility by implementing a middleware to handle www removal across all requests. Create a middleware, such as RedirectToNonWwwMiddleware, and integrate it into your application:

<?php

namespace App\Http\Middleware;

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

class RedirectToNonWwwMiddleware
{
    public function handle($request, Closure $next)
    {
        if (substr($request->header('host'), 0, 4) == 'www.') {
            $request->headers->set('host', 'techvblogs.com');
            return Redirect::to($request->path());
        }
        
        return $next($request);
    }
}

After creating the middleware, seamlessly integrate it into your requests for comprehensive www removal.

Configuring Middleware in Laravel

Ensure your middleware is applied to the desired requests. You can add it globally to all requests or selectively to web-related requests. Modify your Kernel.php file accordingly:

// Applying to all requests
protected $middleware = [
    // ... other middleware
    \App\Http\Middleware\RedirectToNonWwwMiddleware::class,
];

// Applying only to web-related requests
protected $middlewareGroups = [
    'web' => [
        // ... other middleware
        \App\Http\Middleware\RedirectToNonWwwMiddleware::class
    ],
    'api' => [
        // ... other middleware
    ],
];

By incorporating this middleware, you ensure a consistent non-www representation in your web results.

Handling Static Files

When dealing with static files like images or scripts, be mindful of potential issues. Implement htaccess or Nginx redirects to avoid serving these files from the www version, guaranteeing a seamless user experience.

Choosing the Right HTTP Status Code

When implementing the redirection, it's vital to use the appropriate HTTP status code. For a permanent switch to non-www, employ the 301 status code. If a temporary redirect is preferred, utilize the 302 status code.

By adopting these strategies, you not only enhance your website's aesthetics but also optimize its search engine visibility. Remove the www clutter and welcome a streamlined, user-friendly URL structure for improved online success.

Comments (0)

Comment


Note: All Input Fields are required.