How to change the www prefix in domain URLs

This article explains how to add or remove the www prefix from your domain's URLs. For example, if people type www.example.com into their browsers, you can instantly redirect them to example.com, or vice versa.

Adding the www prefix to domain URLs

You can automatically add the www prefix to your domain's URLs by using Apache rewrite rules in a custom .htaccess file. To do this, follow these steps:

  1. Make a .htaccess file in the public_html directory. You can change an existing .htaccess file in the public_html directory.
  2. Copy and paste the following lines of text into the .htaccess file:
    # Add www to any URLs that do not have them:
    RewriteEngine on
    RewriteCond %{HTTP_HOST} !^www\.
    RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
  3. Save the .htaccess file.
  4. To test the configuration, navigate to http://example.com, where example.com is your domain name. The browser should be forwarded to http://www.example.com.

Removing the www prefix from domain URLs

You can automatically remove the www prefix from your domain's URLs by utilizing Apache rewrite rules in a custom .htaccess file. To do so, take these steps:

  1. Create an .htaccess file in your public_html directory. If you already have an .htaccess file in the public_html directory, you can modify it.
  2. Copy and paste the following lines of text into the .htaccess file:
    # Remove www from any URLs that have them:
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^www\.
    RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
  3. Replace example.com in the final line of text with your own domain name.
  4. Save the .htaccess file.
  5. To test the settings, navigate to http://www.example.com, where example.com is your domain name. The browser should be forwarded to http://example.com.

It is desirable for Search Engine Optimization (SEO) purposes to have a website reply solely to http://www.example.com or http://example.com, rather than both. Otherwise, search engines will detect duplicate material on www and non-www domains and may penalize the site's ranking.

Was this answer helpful?

Related Articles

'403 Forbidden' error message

Problem You get a "403 Forbidden" error notice when you try to access your site with a web...

How to disable entity tags (ETags)

This article will show you how to stop your website from sending entity tags (ETags) in HTTP...

How to install Apache Tomcat

Apache Tomcat is a well-known and dependable open-source Java web server. Tomcat is a Java HTTP...

How to troubleshoot CGI scripts

If your CGI (Common Gateway Interface) script isn't working, try the following troubleshooting...

How to use keep-alive connections to improve performance

This article explains how to use keep-alive connections to improve performance in Apache. How to...