How to Redirect www to Non-www URLs in Laravel 10?

09-Feb-2024

.

Admin

How to Redirect www to Non-www URLs in Laravel 10?

Hello Dev,

Now, let's see an example of How to Redirect www to Non-www URLs in Laravel 10. In this article, we will implement a How to Achieve URL Redirection from www to non-www in Laravel. it's a simple example of How to Implement www to non-www URL Redirects in Laravel. it's a simple example of How to Set Up Redirects from www to non-www URLs in Laravel.

In the competitive realm of online presence, ensuring that your website appears in search results as desired is crucial. Many website owners, such as those at Nicesnippets.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.Nicesnippets.com;

return 301 $scheme://Nicesnippets.com$request_uri;

}

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.Nicesnippets.com$ [NC]

RewriteRule ^(.*)$ http://Nicesnippets.com/$1 [R=301,L]

Ensure that 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

{

/**

* Display a listing of the resource.

*

* @return \Illuminate\Http\Response

*/

public function handle($request, Closure $next)

{

if (substr($request->header('host'), 0, 4) == 'www.') {

$request->headers->set('host', 'Nicesnippets.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

],

];

Incorporating this middleware ensures 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.

I hope it can help you...

#Laravel 10