Solved

.htaccess rewrite with multiple domains and subdomains

Posted on 2010-09-07
28
1,154 Views
Last Modified: 2012-06-27
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
Comment
Question by:IndyTechPro
  • 13
  • 11
  • 2
  • +2
28 Comments
 
LVL 21

Expert Comment

by:Julian Matz
ID: 33624615
Hi!

Try this to redirect the subdomains:

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

OR

RewriteCond %{HTTP_HOST} (*\.)?davidandsarahmoore\.com
0
 
LVL 29

Expert Comment

by:fibo
ID: 33624734
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
 
LVL 50

Expert Comment

by:Steve Bink
ID: 33633285
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
 
LVL 27

Expert Comment

by:Nopius
ID: 33633433
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
 
LVL 29

Expert Comment

by:fibo
ID: 33634655
>>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
 
LVL 2

Author Comment

by:IndyTechPro
ID: 33636539
@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
 
LVL 29

Expert Comment

by:fibo
ID: 33636838
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
 
LVL 2

Author Comment

by:IndyTechPro
ID: 33637125
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
 
LVL 29

Expert Comment

by:fibo
ID: 33637834
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
 
LVL 2

Author Comment

by:IndyTechPro
ID: 33637978
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
 
LVL 2

Author Comment

by:IndyTechPro
ID: 33638132
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
 
LVL 29

Accepted Solution

by:
fibo earned 500 total points
ID: 33638392
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
 
LVL 2

Author Comment

by:IndyTechPro
ID: 33638531
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
 
LVL 2

Author Comment

by:IndyTechPro
ID: 33638631
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 29

Expert Comment

by:fibo
ID: 33638859
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
 
LVL 2

Author Comment

by:IndyTechPro
ID: 33639108
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
 
LVL 2

Author Comment

by:IndyTechPro
ID: 33639236
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
 
LVL 2

Author Comment

by:IndyTechPro
ID: 33640535
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
 
LVL 29

Assisted Solution

by:fibo
fibo earned 500 total points
ID: 33642567
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
 
LVL 50

Expert Comment

by:Steve Bink
ID: 33643485
>>> 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
 
LVL 2

Author Comment

by:IndyTechPro
ID: 33650269
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
 
LVL 29

Expert Comment

by:fibo
ID: 33650690
It should, but since this not part of what I write I would need to see your complete script, not just this line.
0
 
LVL 2

Author Comment

by:IndyTechPro
ID: 33650799
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
 
LVL 29

Expert Comment

by:fibo
ID: 33650924
What happens if you remove these 2 extra lines and use my previous script?
0
 
LVL 2

Author Comment

by:IndyTechPro
ID: 33650940
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
 
LVL 29

Assisted Solution

by:fibo
fibo earned 500 total points
ID: 33651155
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
 
LVL 2

Author Closing Comment

by:IndyTechPro
ID: 33651189
This works beautifully! Thanks for all of your time and help with this fibo. I've learned a lot going through the process!
0
 
LVL 29

Expert Comment

by:fibo
ID: 33652259
B-) Glad it works. Thx for the grade and points.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

A/B testing is a simple and effective trick to get to know your audience, increase website conversions and make the most out of your online ad campaigns. It's widely available and doesn't need much tech knowledge to be executed, but the results it y…
Read about how to approach blogging and about ways to do it right. Stand out from the crowd and let your knowledge be consumed by a large audience. This article aims to explain how your blog should look like,  the most important things to do while b…
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now