remove url from htaccess

How to Remove WWW from URL in Apache

Sometimes you may need to remove www from URL in your website. This may be because you want to make it easier for your website visitors to remember your website, or to consolidate www and non-www versions of your website into non-www version for SEO purposes. If you keep www and non-www versions of your website, then search engines may see them as duplicate content and penalize your site. So it is advisable to redirect all www URLs to their non-www versions. In this article, we will learn how to remove www from URL in Apache.


How to Remove WWW from URL in Apache

Here are the steps to remove www from URL in Apache. We will need to enable mod_rewrite module in Apache in order to be able to do this. If you have already enabled mod_rewrite on your Apache server, you can skip to step 2.


1. Enable mod_rewrite

First you need to enable mod_rewrite (.htaccess) for your Apache server. If you have already enabled it, then you can skip this step.

Otherwise, depending on your Linux distribution, run the following commands to enable it.

Ubuntu/Debian

Open terminal and run the following command to enable mod_rewrite.

$ sudo a2enmod rewrite

Redhat/Fedora/CentOS

Open Apache configuration file in a text editor.

$ sudo vi /etc/apache2/httpd.conf
OR
$ sudo vi /etc/httpd/httpd.conf

Look for the following line.

#LoadModule rewrite_module modules/mod_rewrite.so

Uncomment it by removing # at its beginning. If you don’t find this line, add it afresh.

Also look for the following Directory tag and change AllowOverride from None to All.

. . .
<Directory /var/www/html>
. . .
#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# Options FileInfo AuthConfig Limit
#
AllowOverride All
. . .
</Directory>
. . .


2. Open .htaccess file

Open .htaccess file in a text editor. If your .htaccess file is located somewhere other than default location, then replace the path below with the correct file path of .htaccess.

$ sudo vi /var/www/html/.htaccess


3. Remove WWW from URL

Add the following lines to .htaccess file. Replace example.com with your domain.

# Remove www from any URLs that have them:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]

In the above code, the first line is a comment that describes the code snippet. Next line enables mod_rewrite. The third line checks if the requested URL starts with www. The next line redirects all matching URLs to their non-www version.

Save and exit the file.

The above code snippet will redirect all www URLs on your website to their non-www versions. If you want to redirect only some URLs but not all URLs, then here are some specific use cases for your reference


Remove WWW from One Folder

If you only want to remove WWW from one folder, say, /products, then update the above code snippet as shown below.

# Remove www from any URLs that have them:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.example\.com/products
RewriteRule ^(.*)$ http://example.com/products/$1 [R=301,L]

Here we have added /products folder in RewriteCond as well as RewriteRule. So Apache checks if the requested URL contains this folder and also ensures that it is redirected to the same folder’s non-www version.


Remove WWW From One URL

If you want to remove WWW from just one URL,say www.example.com/nike-shoes, then modify the above code snippet as shown below.

# Remove www from any URLs that have them:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.example\.com/nike-shoes$
RewriteRule ^(.*)$ http://example.com/nike-shoes [R=301,L]

In the above code snippet, we have added $ after the URL in RewriteCond to specify that the URL ends there, otherwise Apache will also match URLs starting with folder name as nike-shoes. Similar, we have removed $1 from RewriteURL since we don’t want to append anything after the URL example.com/nike-shoes.


4. Restart Apache Server

Restart Apache server to apply changes.

$ sudo service apache2 restart

Now open browser and go to the www version of your website’s URL (e.g. http://www.example.com). You will see that it redirects you to the non-www version (e.g. http://example.com). In this article, we have learnt how to remove www from URL in Apache using .htaccess.

Also read:

How to Delete Git Tags
How to Drop One or More Columns in Python Pandas
Types of Testing in Python
How to Generate SSH Keys for Git Authorization
How to Download File in Django

Leave a Reply

Your email address will not be published. Required fields are marked *