Redirect non-www to...
 
Share:
Notifications
Clear all

Redirect non-www to www


Neha
 Neha
(@asha)
Member Admin
Joined: 1 year ago
Posts: 27
Topic starter  

I have this in my .htaccess file:

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

but whenever I access a file on my root like http://example.com/robots.txt it will redirect to http://www.example.comrobots.txt/

How can I correct this so that it will redirect correctly to http://www.example.com/robots.txt ?


Quote
Topic Tags
myTechMint
(@mytechmint)
Member Moderator
Joined: 1 year ago
Posts: 29
 

Change your configuration to this (add a slash) to redirect non-www to www

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

OR

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

ReplyQuote