How to remove the www from url with rewite rule

How to remove the www from url with rewite rule

Now these days keeping www in URL is becoming old fashion. How can check many websites for that and the current example is twitter.com. Using apache rule you can remove the www from url with rewite rule.

How to remove the www from url with rewite rule

How to remove the www from url with rewite rule
How to remove the www from url with rewite rule

I specially dont like to put www in URL so we can easily remove the www from URL.

Open your apache configuration file. If you are using Linux then open file from following location

# vim /etc/httpd/conf/httpd.conf

put following lines in your virtual directory section.


# remove www from url
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^.*$ http://example.com%{REQUEST_URI} [R=301,L]

We can use this rewrite rule in .htaccess file also. But make sure .htaccess is enable through apache server.


# remove the www from URL.
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^.*$ http://example.com%{REQUEST_URI} [R=301s,L]

For WordPress websites for seo purpose putting above lines in .htaccess file is really best. That will gives you the really great result in future.

Published by

Purab

I am Purab from India, Software development is my profession and teaching is my passion. Programmers blog dedicated to the JAVA, Python, PHP, DevOps and Opensource Frameworks. Purab's Github Repo Youtube Chanel Video Tutorials Connect to on LinkedIn

Leave a Reply

Your email address will not be published.