How to Redirect www to non-www URLs in Laravel?
In this tutorial, i will show you how to redirect www to non-www urls in laravel application. i will share with you laravel redirect www to non www using htaccess, laravel redirect www to non www using apache2, laravel redirect www to non www using nginx.
Sometime we need to keep only one URL for SEO. we can not open website with www and non-www URLs both for our website. You need to keep all urls with www or non-www URLs. so, i want to give you following four way to redirect www to non-www URL of your laravel app.
1: Laravel Redirect www to non-www URLs using htaccess
To redirect www to non-www URLs in Laravel using .htaccess, you can add the following code to your .htaccess file. Make sure to place this code before any other Laravel-specific rules.
public/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
# Redirect www to non-www
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
# Other Laravel rules...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
This code checks if the HTTP_HOST starts with 'www.' and, if it does, redirects to the non-www version using a 301 permanent redirect. The %1 in the RewriteRule represents the captured group from the RewriteCond condition.
Remember to replace http with https if your site uses SSL. After making changes to your .htaccess file, be sure to test thoroughly to ensure that the redirection is working as expected. Additionally, always make a backup of your .htaccess file before making changes.
2: Laravel Redirect www to non-www URLs using apache2
To redirect www to non-www URLs in Laravel using Apache, you can utilize the VirtualHost configuration. Here's an example of how to configure the redirection in your Apache configuration file:
/etc/apache2/sites-available/example.com.conf
<VirtualHost *:80>
ServerAdmin webmaster@example.com
DocumentRoot /path/to/your/laravel/public
ServerName example.com
<Directory /path/to/your/laravel/public>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
# Redirect www to non-www
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Save the changes to the configuration file.
Restart Apache to apply the changes:
sudo service apache2 restart
Now, you can check your URL.
3: Laravel Redirect www to non-www URLs using nginx
To redirect www to non-www URLs in Laravel using Nginx, you can modify your server block configuration. Here's an example of how to configure the redirection in your Nginx configuration file:
/etc/nginx/sites-available/example.com
server {
listen 80;
server_name www.example.com;
return 301 $scheme://example.com$request_uri;
}
server {
listen 80;
server_name example.com;
root /path/to/your/laravel/public;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
error_log /var/log/nginx/example_error.log;
access_log /var/log/nginx/example_access.log;
}
4: Laravel Redirect www to non-www URLs using Middleware
To redirect www to non-www URLs in Laravel using middleware, you can create a custom middleware to handle the redirection. Here's a step-by-step guide:
Create a new middleware using the following command:
php artisan make:middleware RedirectNonWww
Open the generated middleware file, which is located at app/Http/Middleware/RedirectNonWww.php, and modify the handle method as follows:
app/Http/Middleware/RedirectNonWww.php
<?php
namespace App\Http\Middleware;
use Closure;
class RedirectNonWww
{
/**
* Write code on Method
*
* @return response()
*/
public function handle($request, Closure $next)
{
if (substr($request->header('host'), 0, 4) == 'www.') {
$request->headers->set('host', 'example.com');
return redirect()->to($request->path());
}
return $next($request);
}
}
Register your middleware in the app/Http/Kernel.php file. Add the following line to the $middleware array:
app/Http/Kernel.php
<?php
namespace App\Http;
use Illuminate\Foundation\Http\Kernel as HttpKernel;
class Kernel extends HttpKernel
{
/**
* The application's global HTTP middleware stack.
*
* These middleware are run during every request to your application.
*
* @var array
*/
protected $middleware = [
\App\Http\Middleware\TrustProxies::class,
\Illuminate\Http\Middleware\HandleCors::class,
...
\App\Http\Middleware\RedirectNonWww::class,
];
....
Now, you can check it will work with URLs.
I hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Laravel 10 Eloquent Model Search Query Example
- Laravel JQuery Show Hide Div with Radio Button Example
- How to Send WhatsApp Messages in Laravel 10?
- How to Create PDF using Headless Chrome in Laravel?
- Laravel 10 VUE 3 Axios Get Request Example
- How to Install Laravel 11 Application?
- How to Stop Running Queue Jobs in Laravel?
- Laravel 10 Livewire CRUD Application Example
- How to Send Email using Gmail in Laravel 10?
- Laravel Carbon Count Weekends Days Between Two Dates Example
- How to Write Text on Existing PDF File in Laravel?
- How to add Google ReCaptcha V2 in Laravel 9?
- How to Add Google Map in Laravel?