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" ); ?>

Or ASP:


< % 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.

«

Comments

  1. don
    December 1st

    Ive been trying to figure out how to redirect a bunch of .htm pages I have in the serps to homepage but cant figure our how to use regular expressions to do it so I just used your 404 redirect until I can get some more insight into it.

  2. Jason Hendricks
    December 1st

    Hi Don. I’m no .htaccess expert, but I’ve come across just about every redirect scenario you can think of. Give me more info on exactly what you’re trying to accomplish and maybe I can help.

  3. Richard
    September 16th

    Quick htaccess question:

    I need to redirect all the old pages from a client to the main index page. His old pages are all .htm files. I want to use a wildcard to catch all of the pages. What would be the rule?

    This is what I have so far:

    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTP_HOST} ^domain.com$
    RewriteRule (.*) http://www.domain.com/$1 [R=301,L]

    RewriteCond %{HTTP_HOST} ^www.domain.ca$ [NC]
    RewriteRule ^*.htm$ index.html [NC,R=301,L]

    …but it’s not working. What am I missing?

  4. Richard
    September 16th

    Nevermind… Found the solution:

    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTP_HOST} ^domain.com$
    RewriteRule (.*) http://www.domain.com/$1 [R=301,L]

    RewriteRule ^.*.htm$ http://www.domain.com [NC,R=301,L]

  5. Sanjib
    September 29th

    Hi,

    In a Single IP( windows 2003, iis 6) I have 3 Vitual directory( running 3 dirrent web application.

    On user request, http://www.website.com should redirect to VD1
    on http://www.website.com/VD2 should redirect to VD2 ….

    Like way, I read many Article, non of them satisfactory. your help is highly apreciated.

    Regards

  6. Nicola
    December 22nd

    Hi,
    I’m in a “extremely incompetent hosting company” situation 🙂 that don’t permit me to use my website without DNS.
    Now I’ve put a webserver APACHE on my PC always on with static IP and I’ve forwarded the DNS * and @ to my IP to get the request without www for my website and “redraw” them with WWW.
    I have some problems in doing this.
    For now I’ve tried this:

    but doesen’t work with every page (i.e. http://website.ext/* for every possible string in *).
    Can you suggest me how to fix this problem?
    I repeat, I have an apache webserver so I can also modify settings in apache if is not possible to do this only with PHP.
    Thanks so much.
    PS: It can be better if this fix works independent by domain name, so I can help people with same problem.
    For knowing, the hosting company is TopHost.it

  7. Erik
    January 26th

    Hi, just wondering and new to SEO with URL’s.
    My situation is a bit complex to make it simple for me 😉
    I have a server running IIS6 and using php/mysql for years now. To optimize for search engines, I noticed some techniques like 301 redirects.
    I think I have a trick for my situation, but before making it, I’d like to hear your oppinion about it.

    If I would make urls like: http://mysite.com/my-first-seo-url
    It now would turn into a 404 page. But if I would change the 404.htm into redirect.php in the config, I could then process and query whatever I want to get the right url and make a 301 redirect to it.
    Would that enhance my site for search engines?

  8. Tim
    October 24th

    I have hundreds of links to my blog posts in this format:
    http://www.mysite.com/2008/05/09/my-blog-post/

    but I have changed the settings so the urls now appear like this:
    http://www.mysite.com/my-blog-post/

    Can anyone tell me what do I put in .htaccess to get it old links to re-direct to the new pages? (All is the same, but now they no longer have the date in the url)

  9. Jason Hendricks
    October 26th

    I don’t know if it works with the most current version of WP, but it’s worth a shot:

    http://www.deanlee.cn/wordpress/permalinks-migration-plugin/

    Otherwise you’d have to write a special rewrite rule or manually add an entry for each old page name to redirect to the new page name in .htaccess (tedious).

  10. Peter Benson
    November 3rd

    Thanks for the post, really helpful

  11. vijay
    November 8th

    I have One issue, please help me. My issue is I have url http://subdomain.mydomain.com and want to redirect them http://mydomin.com/folder1/index.php?user=subdomain. but I dont want to change URL in addressbar so in addressbar the url should be http://subdomain.mydomain.com. What i have to do? My .htaccess code is
    =================================
    #RewriteCond %{HTTP_HOST} !^www.mydomain.com [NC]
    #RewriteCond %{HTTP_HOST} ([^.]+).mydomain.com [NC]
    #RewriteRule ^(.*)$ http://mydomain.com/folder1/index.php?user=%1 [L]
    =================================
    It redirect correctly, But Unfortunitly, it affect addressbar, so it’s useless for me. Please help me. What I have to do? Thanks in advance.

  12. Zadling
    March 19th

    Does anyone have a solution to change /category/topic/page/2/ to just /topic/page/2/ and have a wild card for every number? Thanks.

  13. Yes, just use the Redirection plugin for WordPress to handle this. See this post for more info:

    http://www.tidalwavemedia.com/blog/move-your-wordpress-blog-the-seo-friendly-way.htm

  14. Eddie
    May 6th

    Thank you for your great content.

  15. Interesting, Jason…

    I am bookmarking this page as I am sure I may need it in the near future.

  16. Brittni Prueter
    August 17th

    You have a fantastic article here, really helpful. Very well written I shall be bookmarking your website and subscribing to your feed so i can regularly read content of this quality.

Leave a Reply

Free Consultation