301 Redirect Example When Using Wildcard DNS For Subdomains

I know, not another boring post about 301 redirects, but I came across a pretty specific issue recently with one of my sites so I thought I’d post the solution that I finally worked out here. I almost named this post “Why It’s Important To Fail”, but I think most webmasters/SEOs understand that without failure, there can be no success. So I digress…

I couldn’t find a site that specifically addressed the problem I was having, and if the solution is out there, I didn’t see it above the fold as I quickly scanned each site I came across. If I can’t find what I need within seconds, I’m gone.

With that in mind, here’s the code to place in your .htaccess file (apache servers only, you’re on your own with IIS) to 301 redirect everything, including wildcard DNS subdomains and the www version of your domain, to your homepage and do it the right way (I think). An explananation of why I needed to do this and why I believe it’s the correct method in the eyes of the search engines will follow:

RewriteEngine on

RewriteCond %{HTTP_HOST} !^(www).yoursite.com$

RewriteCond %{HTTP_HOST} ^([^.]+).yoursite.com$

RewriteRule .* http://yoursite.com/404.php

It’s important to note that the filename can also be 404.asp or whatever language you prefer. Now, create a new or modify your existing custom 404 file using the following code. I only provided PHP and ASP examples for brevity:

If your site use PHP:

< ? Header( "HTTP/1.1 301 Moved Permanently" ); Header( "Location: http://yoursite.com" ); ?>


< % Response.Status="301 Moved Permanently"; Response.AddHeader("Location","http://yoursite.com/"); %>

Now let me back up for a second. One of my sites died a slow, painful, and miserable death due to a combination of factors, including but not limited to poorly written code slopped over an already questionable framework and an extremely incompetent hosting company. I didn’t choose to sign up with them, but when I purchased the site it was already hosted there and transferring it away was a nightmare due to database size and instability.

At a mere ~5800 users, the database began to self-destruct with corruption and errors, which was surely compounded by the fact that the hosting company couldn’t keep up with the amount of concurrent MySQL connections (even though it was a “dedicated” server). The hosting company said they were automatically making backups of the database and site files nightly, until I needed a restore. All of the sudden they couldn’t find a recent backup. Now of course this is partly my fault for not verifying the integrity (or the existence for that matter) of the backups, but it’s not like I didn’t ask the hosting company about it.

So, I needed a way to let users know about the downtime and what was going on. Also, the site had gained quite a bit of traffic and picked up a lot of link love before it hosed itself, so I didn’t want these links to go to waste. The best thing to do would be to redirect everything to the homepage, but I needed to avoid duplicate content issues. I knew that I needed a 404 to act like a 404, but then 301 to the homepage so that the correct response codes are returned to the search engines.

The site used subdomains for each user via a wildcard DNS setting, so just defining the custom 404 in the .htaccess wouldn’t work. When wildcard DNS is enabled, browsing to http://example.yoursite.com would resolve in the address bar and display the homepage instead of actually redirecting to the homepage URL, which is no good (duplicate content being indexed as this URL).

And that’s when I finally came up with the code above after piecing it all together. Nothing earth shattering or even difficult for those more experienced with redirects than I am, but hopefully someone will find it useful in their journey for a similar fix.



Leave a Reply

Free Consultation