homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

301 redirect?

 7:27 pm on Dec 29, 2012 (gmt 0)

I was just checking my analytics for a small ecommerce site that I own.

Someone came to my site from:


The problem is, I have never set up a subdomain (maybe the previous owner of the website did.....as he was using Yahoo Stores and I'm using Zencart).

When I go to shop.myEcommerceWebsite.com it resolves to myEcommerceWebsite.com.

Should I 301 redirect the subdomain to the index?



 9:53 pm on Dec 29, 2012 (gmt 0)

No. You should not mass redirect all pages in subdomain to the root of the main hostname.

You should redirect pages on the subdomain to the same page on the main hostname.

However, you'll need to be careful how you code this as you could create some sort of infinite loop.


 1:14 am on Dec 30, 2012 (gmt 0)

When I go to shop.myEcommerceWebsite.com it resolves to myEcommerceWebsite.com.

Before you start wantonly redirecting, make sure this subdomain is a one-off. Ask for some random other subdomains and see what happens. You should get an error message from your browser. If some previous owner enabled wild-card subdomains, you need to explore your control panel or equivalent-- I assume this is shared hosting --and turn it off.

Someone came to my site from:


Did they really? A human? When a nonexistent site shows up in the referer slot I normally assume it's a robot. If you have accesss to the raw logs, you might find that they asked for the same page twice, with a redirect on the first attempt.


 2:35 am on Dec 30, 2012 (gmt 0)



I typed in happynewyear.myEcommerceWebsite.com and it resolved to the general index.

The wildcard subdomain is definitely "on", but I'm on a VPS server. 5? other neighbors. This is definitely not from the previous owner, because once I purchased the site, I scrapped the Yahoo Stores design and built everything from scratch using zencart.

So this is most likely a function of the server Admin having wild-card subdomains left "on"?

I want to redirect "shop.myEcommerceWebsite.com" to "myEcommerceWebsite.com" for the simple fact that it's a proper 301 redirect and the landing page is where you would go to start your shopping experience....is this ok?


 6:58 am on Dec 30, 2012 (gmt 0)

If all they ever ask for is the domain, then redirecting to the index page is really the same as redirecting page-for-page. But you might as well code for a full-spectrum page-to-page redirect, even if nobody ever does ask for an inner page. If nothing else, it will protect your against g### getting smart and looking for soft 404s or Infinite URL Space or any of those other things it enjoys so much.

If you can find out where to go or whom to contact to dump the wild-card subdomains, do it. Can't imagine why anyone would need it in e-commerce anyway. Maybe the server admin got so tired of authorizing subdomains site by site, they just switched it on by default.

Now then.

If you don't use any subdomains, the redirect is a freebie because it will run under the code you've already got. Or rather: the code you should already have, so this is a good time to add it.

Your site has one preferred name, say www.example.com. Or maybe example.com without the www. Doesn't matter, so long as there's only one. That means your last redirect in htaccess or the config file will say:

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

If this is happening in the config file rather than htaccess, leave off the / before the $1. Or, if you prefer, express the pattern as ^/(.*)$. The object is to end up with exactly one / in the middle.

g1smd will come along shortly and lay down the law about using mod_rewrite instead of mod_alias (Redirect by that name) for any and all other redirects you may happen to have. He may also, ahem, spot a typo in the rule above, so don't cut-and-paste just yet.

Oh, and as long as you're putting in boilerplate rules: Your second-to-last redirect should be the one that grabs any request for "index.html" (or index.php or whatever you use) and redirects it to plain / at the end. There are at least several hundred posts talking about this one. Some of them even spell out the code. It's just two lines again. Condition and Rule.


 3:54 pm on Dec 30, 2012 (gmt 0)

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

...is good.


 7:22 pm on Dec 30, 2012 (gmt 0)

Hey guys and gals,

Thanks for your help. I reached out to my server admin and had him change the "wild card subdomain" so that everything 404's out.

When you type in: shop.myEcommerceWebsite.com it resolves to:

Server not found
Firefox can't find the server at shop.myEcommerceWebsite.com.


 9:08 pm on Dec 30, 2012 (gmt 0)

Good. That's not a 404, it's a DNS thing. It never reaches your domain at all, so you don't have to deal with it :)


 11:22 pm on Dec 30, 2012 (gmt 0)

The only issue with doing that is that any traffic trying to come in that way no longer sees your site at all.

I would have checked WMT for incoming links and Analytics for traffic before pushing the Big Red Button.


 2:34 am on Dec 31, 2012 (gmt 0)

Hi G1,

Yikes.....since January 400 people have come to my site from the "shop" subdomain.....15 pages average visit.

Should I redirect shop to index?


 6:20 am on Dec 31, 2012 (gmt 0)

If you can do it without the server admin foaming at the mouth ;) resuscitate only the "shop" subdomain. Now that you've got your canonical-site-name redirect in place, anyone who arrives by that route will be safely pointed to the appropriate place.

If they ask for shop.example.com/ and that's all, they'll be sent to your index page.

If they ask for shop.example.com/directory/notherdirectory/longlistofstuff they will be sent to www.example.com/directory/notherdirectory/longlistofstuff.

It sounds as if this will never happen in real life-- but it doesn't matter, because your generic redirect will pick it up anyway.


 11:17 am on Dec 31, 2012 (gmt 0)

Deleting the DNS for the subdomain is the equivalent of placing a sign on the side entrance of a B&M store saying "this business has closed down", rather than the intended "this side-door is no longer in use, please use the main entrance".


 5:49 pm on Dec 31, 2012 (gmt 0)

The tricky bit was that it wasn't just the one subdomain: the site also allowed wild-card subdomains. You can just picture g### getting in a mood and starting to ask for gibberish.example.com. Luckily it is easy to redirect everyone when you don't have any working subdomains to exclude.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved