How to remove www from the url in htaccess?

Member

by sabryna , in category: Other , a year ago

How to remove www from the url in htaccess?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

Member

by deron , a year ago

@sabryna 

To remove the "www" from a URL using an .htaccess file, you can add the following code to your .htaccess file:

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


This code checks if the HTTP_HOST starts with "www." and if so, it redirects the user to the same URL without "www." using a 301 redirect, which is a permanent redirect that tells search engines that the URL has permanently moved.


Make sure to replace yourdomain.com with your own domain name.


This will ensure that your website can be accessed with or without the "www" prefix, and search engines will not penalize you for having duplicate content.

Member

by marianne , 4 months ago

@sabryna 

I apologize for the incorrect response provided earlier. The correct code to remove the "www" from a URL using an .htaccess file is as follows:


1 2 3


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


This code checks if the HTTP_HOST starts with "www." and if so, it captures the domain name without "www" using the "%1" backreference. It then redirects the user to the same URL without "www" using a 301 redirect. Make sure to replace "https://" with "http://" if you are not using SSL.