Knowledge Base
MilesWeb / Domain Name & DNS Issues

Learn to Add or Remove the www Prefix in Domain URLs

Approx. read time : 3 min

The article guides you on how to add or remove the www prefix in your domain’s URLs. For example, if users type www.mydomain.com in their browsers, they automatically get redirected to mydomain.com or vice versa.

While considering Search Engine Optimization (SEO), it is advised that a website should respond only to http://www.mydomain.com or http://mydomain.com, but not both. This is because the search engine considers content on www and non-www domains as duplicate and thus may downgrade the site’s ranking.

# Steps to add www prefix to domain URLs

Using Apache rewrite rules in custom .htaccess file, allows you to add the www prefix to your domain’s URL automatically. Perform the following steps to do so :

1) First, create a .htaccess file in your public_html directory. In case you already have a .htaccess file in the public_html directory, then you can modify it directly.

2) Copy and paste following commands in the .htaccess file

# To add www to any URLs that do not have them use this code:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

3) Now, save the .htaccess file.

4) To test the configuration, using a web browser visit http://mydomain.com, where mydomain.com represents your domain’s name. The browser should get redirected to http://www.mydomain.com

# Steps to remove the www prefix from domain URLs

Apache rewrite rules in a custom .htaccess file allows you to automatically remove the www prefix from the domain’s URLs. Perform the following steps to do it :

1) First, create a .htaccess file in your public_html directory. In case you already  have a .htaccess file in the public_html directory, you can modify it directly.

2) Copy and paste following commands in the .htaccess file

# To remove www from any URLs that have them:

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

3) Make sure you replace mydomain.com in the final line text with your domain name.

4) Save the .htaccess file.

5) Now, to test the configuration, using a web browser visit http://www.mydomain.com, where mydomain.com represents your domain’s name. The browser should get redirected to http://mydomain.com

That’s it.

Also Read :

1) Do SSL certificates work with and without www on domain at same time ?

2) Learn to configure caching with the mod_expires module

Sonam Wagh
With an interest in doing something creative daily, Sonam works as a Digital Marketing Executive. She likes to write technical blogs related to web hosting, digital marketing, and other IT topics. She also likes to spend her leisure time on social media to find different strategies for client engagement.
Need help? We’re always here for you.