Redirect www to non www url using htaccess
sSometimes we can access our website on both url like https://www.learncodetoday.com and https://learncodetoday.com, but its not good for SEO. So today we will learn how can we redirect a user from www to non-www url using htaccess.
.htaccess file is a routing file that you can find on your project root folder and if its not exist there then you can create a .htaccess file there.
Copy and paste below code into your .htaccess file for redirection
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
RewriteCond %{ENV:HTTPS} !on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Now save file and try to open your website by using www url and it will auto redirect you on non-www url.
Related Links
Comments
Post a Comment