Are your subdomains in order?

I was recently trying to recall a subdomain (for example: http://subdomain.stormyfrog.com/) I had set up for a client, and tried guessing at what I thought it was. While I guessed wrong, it highlighted an important issue to address.

Wildcard DNS

The subdomain I tried was not an actual site for the client, but a wildcard DNS entry was sending it to the IP of the server anyways. If you have a wildcard entry set up for your domain(s) you should take care to see what your web server is serving up for subdomains you aren’t actually using. In most cases, it will serve up the default site for that IP address, but under whatever URL you used.

Fixing the issue

Once you know what site the server is serving up for your invalid subdomains, add or edit the .htaccess file for that site to include a redirect for any URLs not matching the correct domain. This way you’ll avoid any potential duplicate content issues and possible SEO penalties.

Option 1: Redirect any page requests to main website homepage

This option would send any visitors from http://subdomain.stormyfrog.com/awesome-page/ back to the homepage at http://www.stormyfrog.com/.

RewriteEngine on
RewriteCond %{HTTP_HOST} !www\.stormyfrog\.com [NC]
RewriteRule .* http://www.stormyfrog.com/ [R=301,L]

Option 2: Redirect page requests to equivalent page on correct domain

This option would send any visitors from http://subdomain.stormyfrog.com/awesome-page/ to http://www.stormyfrog.com/awesome-page/.

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

Option 3: Combined approach

This is the approach I personally use. Anyone entering the site with wwww (four w, a common mistake) or no www is redirected in a manner like Option 2 sending them to the right page. Anyone else making up subdomains or even using other domain names I may have pointing at the same IP but am not using would be sent to the homepage.

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

Do you prefer no www for your site?

If you prefer your site running as http://stormyfrog.com/ rather than http://www.stormyfrog.com, use the variations below instead.

Option 1 for no-www

RewriteEngine on
RewriteCond %{HTTP_HOST} !^stormyfrog\.com [NC]
RewriteRule .* http://stormyfrog.com/ [R=301,L]

Option 2 for no-www

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

Option 3 for no-www

RewriteEngine on
RewriteCond %{HTTP_HOST} ^wwww?\.stormyfrog\.com [NC]
RewriteRule ^(.*)$ http://stormyfrog.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} !^stormyfrog\.com [NC]
RewriteRule .* http://stormyfrog.com/ [R=301,L]

Notes

This will only work on Apache-based web servers with security settings that permit you to override FileInfo settings in your htaccess file.

Share Your Thoughts

Hi, is and , which you swear not to share with anyone, is .
If you're interested, I also have you can read at (optional).
Below are for this post: