Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.htaccess redirect - automatically add www. if no subdomain exists

I need any www. added automatically to my domain ONLY IF a subdomain is not already there. I do want subdomains to bypass this redirect.

How can I do this?

like image 390
kdjernigan Avatar asked Sep 04 '12 02:09

kdjernigan


People also ask

How can I redirect and rewrite my urls with an .htaccess file?

Use a 301 redirect . htaccess to point an entire site to a different URL on a permanent basis. This is the most common type of redirect and is useful in most situations. In this example, we are redirecting to the "example.com" domain.


1 Answers

To automatically add a www to your domain name when there isn't a subdomain, add this to the htaccess file in your document root:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]
like image 178
Jon Lin Avatar answered Nov 27 '22 07:11

Jon Lin