[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1210
  • Last Modified:

.htaccess rewrite with multiple domains and subdomains

Hi there,

I am new to using htaccess files. I have a single hosting plan on an IIS server using IIS Mod-Rewrite Pro Edition from Micronovae, so it should work the same as the Apache mod_rewrite.

I have multiple domains on a single hosting plan. Each site is contained within a separate sub-directory. Previously, to get a given domain to redirect, I wrote a PHP redirect script as the root index.php file. This has worked fine, but I am realizing the importance of hiding the sub-directories and listing only the domain name for search engine purposes, so the PHP script doesn't cut it.

I have tried 20+ examples throughout the net attempting to make this work, but have had little success.

Here is the basic code I started with. This works to redirect the domains, but it grabs the entire domain, so sub-domains are not directed accordingly:

#
RewriteEngine On
RewriteCond %{HTTP_HOST} indytechpro.com
RewriteCond %{REQUEST_URI} !itp2/
RewriteRule ^(.*)$ itp2/$1 [L]
#
RewriteCond %{HTTP_HOST} davidandsarahmoore.com
RewriteCond %{REQUEST_URI} !dsm/
RewriteRule ^(.*)$ dsm/$1 [L]
#
Additional domains follow...

Open in new window


Any help is appreciated!
0
IndyTechPro
Asked:
IndyTechPro
  • 13
  • 11
  • 2
  • +2
3 Solutions
 
Julian MatzCommented:
Hi!

Try this to redirect the subdomains:

RewriteCond %{HTTP_HOST} *\.davidandsarahmoore\.com

OR

RewriteCond %{HTTP_HOST} (*\.)?davidandsarahmoore\.com
0
 
fiboCommented:
Not sure you are not simply trying to implement virtual hosts... one host for each domain...
I would think that IIS provides you with different solutions, where on receiving a call to indytechpro.com
 it places you directly into the itp2 directory.

So this is not really an htaccess problem, but rather a IIS/ virtual hosts problem.

As a rule of thumb, htaccess is usually for "intra domain" rewriting
0
 
Steve BinkCommented:
I agree with fibo.  

Are you in a shared or dedicated environment?  If your host provides a dedicated server, there is absolutely no reason for you to be configuring the service like this.  If you are in a shared environment, you would only need to do this to not have to pay your host for the additional domains.

Either way, you're going about this wrong.  Each domain should be its own site.
0
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
NopiusCommented:
If it was working with PHP this can also be done in .htaccess (even though IIS specific virtual domain configuration is preferable).

>  This works to redirect the domains, but it grabs the entire domain, so sub-domains are not directed accordingly:

If you mean that all subdomains should be directed to the same folder as main domain, I see no errors in your config. If you mean something different, please elaborate with examples.

Regards,
Arty
0
 
fiboCommented:
>>sub-domains are not directed accordingly
they will need their own redirect as well.
in fact the site is a subdomain on its own, usually working with and without the www.

If you want to redirect a given subdomain, place the rewrite in lines before the ones for the domain
0
 
IndyTechProAuthor Commented:
@routinet:

>> If you are in a shared environment, you would only need to do this to not have to pay your host for the additional domains.

Not true. Not only does my host support multiple domains under a single account, they are the ones who recommended using .htaccess to provide this functionality.

This is not a dedicated server, but a shared hosting environment. Creating individual sites in IIS would be ideal, but I do not have this capability with the hosting plan.

I will try to clarify the question a bit. I guess when you break it down, I am trying to accomplish 3 things here:

#1 - Redirect the domain and each sub domain to appropriate, separate folders. For example, www.indytechpro.com and indytechpro.com should both redirect to the folder doc_root/itp2/, but if I want to add a sub domain, such as forums.indytechpro.com, that should redirect to it's own folder, like doc_root/itpforums/

#2 - Rewrite the domain name in the web browser so users and search engines see "indytechpro.com" instead of "www.indytechpro.com/itp2/index.php". This should work whether they use the www prefix or not.

#3 - Provide the same functionality for any additional domains pointed to this hosting plan

Thanks for the help
0
 
fiboCommented:
Seems you are taking a difficult route for something that should be trivial.

Is there some sort of FAQ for your hosting plan? can you point us to it?

In the DNS, how are defined the domains that point to your site? More precisely, what is the content of their "Record A" (sorry, but this needs to be checked)

For your item #1, I suggest you do as per my previous suggestion: place lines with the subdomains rewrites (1 for each subdomain) before the full domain rewrite

0
 
IndyTechProAuthor Commented:
Sure fibo, here is a link to the knowledgebase post from my hosting provider. This is where I got the .htaccess code I originally posted:
http://help.webcontrolcenter.com/KB/a786/how-can-i-host-multiple-websites-under-single-hosting.aspx?KBSearchID=122208

The host is CrystalTech (http://www.crystaltech.com). I have been using them for many years with a single shared hosting plan and multiple domains in use.

DNS A Records (For domain indytechpro.com). Each domain has it's own DNS control within my hosting plan, but all are basically the same:
@      216.119.73.30
*      216.119.73.30
www      216.119.73.30
mail      216.119.115.139
0
 
fiboCommented:
Can you test the impact of
#
RewriteEngine On
#
RewriteCond %{HTTP_HOST} forums.indytechpro.com
RewriteCond %{REQUEST_URI} !itpforums/
RewriteRule ^(.*)$ itpforums/$1 [L]
#
RewriteCond %{HTTP_HOST} indytechpro.com
RewriteCond %{REQUEST_URI} !itp2/
RewriteRule ^(.*)$ itp2/$1 [L]
#
RewriteCond %{HTTP_HOST} davidandsarahmoore.com
RewriteCond %{REQUEST_URI} !dsm/
RewriteRule ^(.*)$ dsm/$1 [L]
#

Open in new window

0
 
IndyTechProAuthor Commented:
I tried the example and get a 404 The Page Cannot Be Found. If I include R=301 to make this a redirect, I believe it then shows me the full URL it is directing to and the URL comes out as http://forums.indytechpro.com/itpforums/itp2/
0
 
IndyTechProAuthor Commented:
Scratch that, I must have made a mistake somewhere. I recreated the file and got the expected results using forum.indytechpro.com. However, if I try to add an additional nested subdirectory, I get the same result as my previous post:

"kb.indytechpro.com" becomes "kb.indytechpro.com/itp/kb/itp2"

this is the block I attempted in the htaccess:

[code]
#
RewriteCond %{HTTP_HOST} kb.indytechpro.com
RewriteCond %{REQUEST_URI} !itp/kb/
RewriteRule ^(.*)$ itp/kb/$1 [L,R=301]
#
[/code]
0
 
fiboCommented:
Mhhh... seems the "catch all rewrite" captures the rewritten url... can you test the revised script

YOU DO NOT WANT R=301 in your case, because probably it would not index forums.xxx.com, but www.xxx.com/itpforums


#
RewriteCond %{HTTP_HOST} forums.indytechpro.com
RewriteCond %{REQUEST_URI} !itpforums/
RewriteRule ^(.*)$ itpforums/$1 [L]
#
RewriteCond %{HTTP_HOST} kb.indytechpro.com
RewriteCond %{REQUEST_URI} !itp/kb/
RewriteRule ^(.*)$ itp/kb/$1 [L]
#
RewriteCond %{HTTP_HOST} indytechpro.com
RewriteCond %{HTTP_HOST} !forums.indytechpro.com
RewriteCond %{HTTP_HOST} !kb.indytechpro.com
RewriteCond %{REQUEST_URI} !itp2/
RewriteRule ^(.*)$ itp2/$1 [L]
#

Open in new window

0
 
IndyTechProAuthor Commented:
Right, sorry, I only included the 301 to demonstrate how I "saw" the URL it was outputting.

This code example has the same result. The strange thing is, forums.indytechpro.com works and redirects to the proper folder without adding /itp2/, but kb.indytechpro.com is still adding it. Does this have something to do with the fact the directories are nested 2 levels deep?
0
 
IndyTechProAuthor Commented:
I've done additional testing and if I change the directory from itp/kb to itpkb for example, the rewrite works. As soon as I add the / in the middle, it appends the /itp2/ on the end.
0
 
fiboCommented:
I'm trying a slightly different test
#
RewriteCond %{HTTP_HOST} forums.indytechpro.com
RewriteCond %{REQUEST_URI} !itpforums/
RewriteRule ^(.*)$ itpforums/$1 [L]
#
RewriteCond %{HTTP_HOST} kb.indytechpro.com
RewriteCond %{REQUEST_URI} !itp/kb/
RewriteRule ^(.*)$ itp/kb/$1 [L]
#
RewriteCond %{HTTP_HOST} indytechpro.com
RewriteCond %{REQUEST_URI} !itpforums/
RewriteCond %{REQUEST_URI} !itp/kb/
RewriteCond %{REQUEST_URI} !itp2/
RewriteRule ^(.*)$ itp2/$1 [L]
#

Open in new window

0
 
IndyTechProAuthor Commented:
This is a bit strange. I got the same results from the latest code. However, if I change the folder name from itp/kb to itp3/kb, test/kb, whatever, it works. Only when it's itp/kb does it append the extra "itp2" on the end. This is also true for the previous code (Where you excluded by domain name instead of folders). Why would only this combination cause this?
0
 
IndyTechProAuthor Commented:
Found the problem! There was a conflicting .htaccess file in the itp/kb directory which has been fixed. Sorry for the time spent in the wrong direction!

However, now that the redirect works to point to the correct folder, other functions are not working with the site, such as the CSS and images not loading. I realize this is because something is happening with the redirection of the URL. But it seems I cannot access sub-folders of the domain manually. When I try to log in to the forum, it jumps to the URL http://www.indytechpro.com/itp/kb/index.php?action=login2, which dies at a 404 error. This is the correct location, but my guess is the mod_rewrite is changing the indytechpro.com behind the scenes and trying to look for /itp/kb/index.php?action=login2 within the itp2 folder. How do I prevent this sort of issue?
0
 
IndyTechProAuthor Commented:
OK, I think I have sorted out the issues with images loading, etc. This was an application issue as the forum required updating it's configuration to match the "new" domain (kb.indytechpro.com) instead of routing everything through the original path (www.indytechpro.com/itp/kb).

Next, I wanted all traffic to indytechpro.com to 301 to www.indytechpro.com. I believe I can accomplish this by adding the following to the .htaccess:

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

Is there anywhere special this needs to fall within the file, or should it go at the top to be the first thing processed? Also, it doesn't seem to catch the "unknown" sub-domains. For example, I would like anything that isn't explicitly defined in the .htaccess file to point to http://www.indytechpro.com, (whatever.indytechpro.com). Thanks
0
 
fiboCommented:
You might try something like

[code]
RewriteCond %{HTTP_HOST} !^www.indytechpro.com [NC]
RewriteCond %{HTTP_HOST} !^forums.indytechpro.com [NC]
RewriteCond %{HTTP_HOST} !^kb.indytechpro.com [NC]
RewriteRule ^(.*)$ http://www.indytechpro.com/$1 [L,R=301]
[/code]
0
 
Steve BinkCommented:
>>> Not true. Not only does my host support multiple domains under a single account, they are the ones who
>>> recommended using .htaccess to provide this functionality.

Totally true.  I work for a hosting company, and we tell customers the same thing when they don't want to pay for additional hosting plans - just put it under your current account.  That does not mean it is the easiest method to make this happen, nor does it mean it is the "proper" way to do it.  Many come back to us after a few months to create a second hosting account due to the amount of confusion it causes on their end.

When a basic shared hosting plan for a single domain costs less than $5/month (and that is on the expensive side), it seems a bit senseless to put this much effort into a rigged solution that only serves to complicate your environment.

In any case, it looks like you and fibo ironed out the details.  Good luck!
0
 
IndyTechProAuthor Commented:
Fibo, that code works, but works a little too well. It grabs all of my domains and sends them to www.indytechpro.com

I guess I am not understanding why the following line does not work:

RewriteCond %{HTTP_HOST} ^indytechpro.com [NC]

Shouldn't this get any sub-domain, but only from the indytechpro.com domain?
0
 
fiboCommented:
It should, but since this not part of what I write I would need to see your complete script, not just this line.
0
 
IndyTechProAuthor Commented:
It is the same line from my post on 9/9/10 at 3:29PM. Here are the two lines working together:

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

Here is the complete expression up to this point:
#
RewriteEngine On
#
RewriteCond %{HTTP_HOST} ^indytechpro.com [NC]
RewriteRule ^(.*)$ http://www.indytechpro.com/$1 [L,R=301]
#
RewriteCond %{HTTP_HOST} kb.indytechpro.com
RewriteCond %{REQUEST_URI} !itp/kb/
RewriteRule ^(.*)$ itp/kb/$1 [L]
#
RewriteCond %{HTTP_HOST} indytechpro.com
RewriteCond %{HTTP_HOST} !kb.indytechpro.com
RewriteCond %{REQUEST_URI} !itp/
RewriteCond %{REQUEST_URI} !itp2/
RewriteRule ^(.*)$ itp2/$1 [L]
#

0
 
fiboCommented:
What happens if you remove these 2 extra lines and use my previous script?
0
 
IndyTechProAuthor Commented:
Sorry, I am not sure I follow. I tried using this:

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

But this is the script that catches all domains, not just sub domains of indytechpro.com. So I reverted back to this:

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

Simply because it at least catches indytechpro.com and adds the www prefix. But it doesn't catch anything else for some reason (any other sub domains). Thanks
0
 
fiboCommented:
OOps. Sure it catches all domains... I forgot there are other domains different from indytechpro and my "catch all"... does

Not sure but simply using

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

Should do the trick

0
 
IndyTechProAuthor Commented:
This works beautifully! Thanks for all of your time and help with this fibo. I've learned a lot going through the process!
0
 
fiboCommented:
B-) Glad it works. Thx for the grade and points.
0

Featured Post

Granular recovery for Microsoft Exchange

With Veeam Explorer for Microsoft Exchange you can choose the Exchange Servers and restore points you’re interested in, and Veeam Explorer will present the contents of those mailbox stores for browsing, searching and exporting.

  • 13
  • 11
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now