How to add WWW to URL in php

If your site can be accessed as both as well as and are having trouble with things such as cookies on the Netscape browser*, here is a little rewrite rule you can place in your httpd.conf or on a per directory basis using .htaccess

Here is what happens. When you request a page such as, the rewrite engine checks to see if the HTTP_HOST does not match the fully qualified domain name which in this case is If they do not match, the engine rewrites the URL with the SERVER_NAME value.

#Redirect non-www to www
RewriteEngine On

RewriteCond %{HTTP_HOST} ^$ [NC]
RewriteRule ^(.*)$$1 [R=301,L]

Create .htaccess file and write above code in this file. Access your domain as, www will add automatically with the domain like

Was this post helpful?
Let us know if you liked the post. That’s the only way we can improve.
Harinder Singh

Harinder Singh

My name is Harinder Singh and I specialize in Software industry. I consider myself as a life learner. I love learning new concepts, embracing new ideas and reading and searching for innovation.