user532493
user532493

Reputation: 345

.htaccess Only for www. Subdomain

How do I construct my .htaccess file so that it adds www to the beginning of the address mysite.com, but doesn't add www. to the beginning of other subdomains such as test.mysite.com? My .htaccess file currently looks like this:

Options -Multiviews
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php

Upvotes: 1

Views: 136

Answers (2)

Mathieu Dumoulin
Mathieu Dumoulin

Reputation: 12244

I just don't know the darn rule to get only the characters for domain naming so i'll try this but it might fail on some subjects:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^[a-zA-Z0-9\-]+(\.[a-z]{2,4})+$
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php

This will force all domains that START with one word and a TLD to be forwarded to www.samedomain.tld but subdomains wont be hit...

Upvotes: 0

Wrikken
Wrikken

Reputation: 70500

Something like:

RewriteCond %{HTTP_HOST} ^mysite.com

Instead of

RewriteCond %{HTTP_HOST} !^www\.

Upvotes: 2

Related Questions