Best practices for blocking multiple entry paths to addon domain?

Hello. I have 7 or so addon domains. To those that don’t know this is a hostgator option to add more domains off your main domain. This is not a subdomain however the folder structure is still setup as if it was a subdomain. Its a Cpanel error that they need to fix.

So there are three ways to get to the addon domain.

addondomain.maindomain
maindomain/subdomain
subdomain

In theory unless you have links floating around google wont ever see these. Thats not good enough for me. So whats the best way you think to redirect or block these alternate paths? I have the code I just need your thoughts on best practice. Should I block them altogether with robots.txt or should I instead 301 redirect them? And I guess I would have to do this on each site and not just the main domain right? Or am I missing a easier way?

Thanks!
Eric W

Eric,

I’m a believer in the power of mod_rewrite so that’s what I’d use. Using pseudo code:

If {HTTP_HOST} is NOT subdomain
AND the subdomain directory is requested
Redirect to http://www.subdomain.com without the subdomain directory as a permanent redirect.

In mod_rewrite code:

# in maindomain's DocumentRoot

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

While you’ve crossed your addondomain and subdomain terms, I’m sure you know which is which. The only problem I can see would be if the subdomain requires SSL … but that can be handled in the subdomain’s DocumentRoot and/or scripts requiring SSL.

Regards,

DK

Ha i was like “where?!”. Ya I see it so I did. maindomain/addondomain it should be. I don’t think I have ever written anything 100% correct the first time I write it. I always have to catch it in the proof.

Thanks for your reply :slight_smile: So I have this canonical code in my htaccess which I believe your provided for me a while back. Using the redirect you showed above, would I also have to redirect all versions of non-www as well or would my existing code already provide that function? Looking closer at the code you just showed it looks like it does the non-www too is that correct?

# Rewrite index.html/php to folder - First line excludes Forum
RewriteCond $1 !^forum/adm(?:$|/)
RewriteRule ^(.*/)?index\\.(html?|php)$ /$1 [R=301,L]

# Rewrite non-www (non-canonical) to www - First line excludes Forum
RewriteCond $1 !^forum/adm(?:$|/)
RewriteCond %{HTTP_HOST} !^www\\. [NC]
RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [R=301,L]

Also. I do not want Google thinking by mistake that any of the subdomains are in anyway associated with the maindomain. Brains a bit rusty so I must confirm? If using either robot.txt blockage and 301 redirect Google cannot read those redirects correct? Forinstance it makes me parinoid to even have one of those redirects in my maindomains htaccess. From what I know/think is that any rediirection happens before google spiders can get their eyes on it correct? I just need to be 100% correct on this. Thats why I was thinking of a strait blockage with the robot.txt - like saying it doesnt even exist.

EDIT - actually I think htaccess redirects happen before google can see it. But robot.txt google does read but honors what it reads. So how can it honor it if it doesnt read it. Thats my guesstament anyway. This guy gives his perspective on it http://smepals.com/google-panda/when-use-robots-txt-301-redirects-panda-seo-recovery

Actually it may just give me give me piece of mind to simply get another hosting server for 70 bucks a year to host all my additional domains. I will entertain the idea.

…and additionally I seee you say put that code in the maindomains htaccess. It doesnt need to go in all the addondomains htaccess as well???

PT,

Sounds like something I’d do! No worries.

The code I used above made the www optional so that’s already handled (and does not force www on the non-www’d version).

# Rewrite index.html/php to folder - First line excludes Forum
RewriteCond $1 !^forum/adm(?:$|/)
RewriteRule ^(.*/)?index\\.(html?|php)$ /$1 [R=301,L]
[indent] All that optional? Why not simply:
RewriteCond %{REQUEST_URI} !(forum|adm)
RewriteRule ([a-z]+/)index\\.(html?|php)$ $1 [R=301,L]
Please remember that I'm allergic to (.*) unless you want to capture EVERYTHING/NOTHING.
htm, html and php?!?[/indent]

# Rewrite non-www (non-canonical) to www - First line excludes Forum
RewriteCond $1 !^forum/adm(?:$|/)
RewriteCond %{HTTP_HOST} !^www\\. [NC]
RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [R=301,L]
[indent]Why exclude forum and admin? 
Why the extraneous regex?[/indent]

Where’s the redirection of the addondomain in all this?

Regards,

DK

PT,

That’s why you’d redirect TO the addondomain(s)! Google (et al) should read the 301 redirects - that’s what they’re there for!

I’m glad you’ve got more money (than sense?!?). Use mod_rewrite to resolve this very simply.

Not necessary for maindomain/addondomain but, if addondomain.maindomain physically goes directly to maindomain/addondomain as its DocumentRoot, YES (I’d overlooked that possibility - good pick-up!).

Regards,

DK

I didn’t have your add on domain code in there that’s all. Sense?!?! So you think that it would be stupid to buy new hosting account?

“Not necessary for maindomain/addondomain but, if addondomain.maindomain physically goes directly to maindomain/addondomain as its DocumentRoot, YES (I’d overlooked that possibility - good pick-up!).”

I didn’t know I picked it up lol. So would your code you showed before differ now if that’s the case! Thanks bro for your help.

PT,

No, not stupid! Just rich! Solving an easy problem by throwing money at it when it’s not needed is what I was commenting on. That would have been an easy solution but, as I said, it’s just not necessary.

Well, your question DID pick it up and you got kudos for that - deservedly so. I know better than to overlook things like that (DocumentRoot’s physical location is important!).

Yes, it should work (unless there are other things “under the hood” that I’m not aware of). Using RewriteCond statements to examine the {HTTP_HOST} can solve domain name problems when there are several domains using the same webspace (e.g., it helps me to change from US$ to NZ$ as appropriate within a script).

Regards,

DK

I found the original thread where I got that conocalzation code. You were in it (the first post) but it looks like Jeff mot finished it up. http://www.sitepoint.com/forums/showthread.php?1013274-Need-help-tweaking-this-htacces-code-please.

So how would you clean up or better that code? I had to remove the forum from it because otherwise I couldn’t get into the admin page. Apparently it writes to the index page. So if I redirect to folder then no more index page to write to. I’m not appossed to doing it your way with regards to the addon domain. Still tooling around with how to proceed. If I were to buy two accounts and pay three years on advance then it actually comes to the same price I’m paying now.

PT,

Jeff and I have a severe difference of opinion about many things so, if he’s in a thread, I’ll leave. For the most part, though, he does give good information.

How would I clean-up the code? Since my mind (at least in the forum) is like a web page (stateless - I need to be refreshed every time), I’ll just comment on the block of code (again) and await a full “specification” and the mod_rewrite code you’re trying to use:

# .htaccess in DocumentRoot

# First task - force www on the domain - I'll include the forum and adm, though, for URI consistency 

[COLOR="#A9A9A9"]# Rewrite non-www (non-canonical) to www - First line excludes Forum[/COLOR]
RewriteCond $1 !^forum/adm(?:$|/)
# $1 is the {REQUEST_URI} variable so I'd use that instead
# (?:$|/) is some weird regex - it appears to try to match an end of string OR / (with possible other information behind it, i.e., no end anchor)
# I'd simply use RewriteCond %{REQUEST_URI} !^(forum|adm)
# Note, though, that this would PREVENT the force-www if forum or adm are requested which breaks your standardization
#      so I'd recommend just dropping this RewriteCond entirely
RewriteCond %{HTTP_HOST} !^www\\. [NC]
# Perfect - unless you have subdomains which could then be redirected to www.subdomain.domain.com
#      so I would recommend using your domain name after the "\\." - be sure to keep the No Case flag, though!
RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [R=301,L]
# Minor point but, again, (.*) is the ALL or NOTHING which happens to be Apache's {REQUEST_URI} variable so I'd simplify and use
# RewriteRule .? http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

# SECOND Task: Strip index.{whatever} from the URI
# Note, please, that your SysAdmin may have setup Apache to display the DirectoryIndex
#       even when it's not requested ... you can't fight City Hall and you can't overrule httpd.conf!

# Rewrite index.html/php to folder - First line excludes Forum
RewriteCond $1 !^forum/adm(?:$|/)
# Ditto the above
# RewriteCond %{REQUEST_URI} !^(forum|adm) # recommended
RewriteRule ^(.*/)?index\\.(html?|php)$ /$1 [R=301,L]
# Will you accept ANY characters before index.{whatever}? If not (and you're only using lowercase letters)
# RewriteRule ([a-z]+/)+index\\.(html?|php)$ $1 [R=301,L]

I hope you’re half as concerned about your website’s security as I am about mine. If so, you’ll not use wildcards indiscriminately and either specify the acceptable names ( like (forum|adm) ) or characters ( like [-a-zA-Z0-9_/]+ for hyphens, upper and lowercase letters, digits, underscores and slashes ). If you have a tool as powerful as mod_rewrite to help. Just don’t let your scripts scrimp on similar checks, though!

Regards,

DK

Unfortunately my skills here arent good enough to know the answers to any questions above or to edit them appropriately. :frowning:

I sort of convoluted this thread a bit. But just to confirm, since you mentioned a caught a over site, is this following code all I would need to redirect the addon domain from all the different entry paths? And wouldnt I also have to put said code in the main site and the addon domains htaccess’s? Thanks!

# in maindomain's DocumentRoot

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

OK i just tried using this but it seemed to have no effect - still went to the site via the additional paths.

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

Strangley even before I put in the htaccess code I could only get to it via two paths

http://addondomain.com/ (As an Addon Domain Name)
http://primarydomain.com/addondomain.com/ (As a subfolder)

http://addondomain.com.primarydomain.com/ (As a Subdomain Name) would come back as a 404. Am I missing something?

PT,

I tried to be “too cute” with my use of maindomain’s DocumentRoot for the majority of the mod_rewrite work for subdomain. Upon reconsideration, it would be simpler to do as you suggested and put all the mod_rewrite in the subdomain’s DocumentRoot (where you don’t need to worry about other subdomains).

# in SUBdomain's DocumentRoot

RewriteCond %{HTTP_HOST} !^(www\\.)?subdomain\\.com$ [NC]
# Note: Only requests of the subdomain, even via maindomain, will have access to the subdomain directory
RewriteRule .? http://www.subdomain.com/$1 [R=301,L]

Since you’re no longer concerned with conflicts with maindomain or any other subdomain, this is by far the simplest (and most effective) approach.

Regards,

DK

Cool thank you I will try that. Any idea why my addon domain is not assessable via http://addondomain.com.primarydomain.com/. Is it maybe because of the non cononcol link code I have in there that I referenced above in the beginning?

Also what are your thoughts on mod_rewrite vs 301 redirect for this purpose? Rewrite seems to me better suited.

PT,

The obvious reason is that the subdomain URL for addondomain will not have two .com’s in it.

301 is a type of redirect which is available to both mod_rewrite and mod_alias (which is what you’re trying to compare it with). For me, mod_rewrite has a lot more power (it can use RewriteCond statements to check Apache variables other than the URI) while mod_alias (at least the Redirect function) does not require regular expressions (not loading the regex engine makes mod_alias faster for simple redirections). I will use both (an early thing you should learn about mod_rewrite is when NOT to use it) as required by the situation but, with a knowledge of the simple regex used by mod_rewrite, it’s such a powerful tool that I tend to lean on it as if it’s a crutch (I KNOW it works!).

Oh? “… for this purpose”? mod_alias can’t examine the {HTTP_HOST} so it’s not even in the running.

Regards,

DK

You just mixed up terms here like I did right? I assume it matters zero in the end code? Sorry to be obsessive it’s just url rewrites are so important I have to be 10%.

Oh and I did try it without the .com http://addon.maindomain.com as well. Just 404.