homepage Welcome to WebmasterWorld Guest from 54.166.122.65
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

    
Apache Reverse Proxy subdomains to subfolders
apache rewrite subdomains subfolders
jackx



 
Msg#: 4567684 posted 9:34 pm on Apr 24, 2013 (gmt 0)

Greetings,

Have searched and have not yet quite found what I was looking for, so I'm posting and hoping that someone could offer some guidance and perhaps an example to work from - I am fairly new to Apache rewrites and reverse proxies

In Apache 2.2, I would like to be able to have 7 different subdomains rewritten to subfolders

Example: subdomain1.domain.com should render as domain.com/subdomain1 in the address bar, when a user types in subdomain1 in the address bar

I have 7 subdomains that I need this to work for, each with their own subdirectory.

Not sure if I need a rewrite for each of the 7 subdomains or whether one rewrite condition can catch all?

many thanks in advance

 

lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4567684 posted 12:43 am on Apr 25, 2013 (gmt 0)

I'll be darned. Usually people say "redirect" when they mean "rewrite", but you've got the opposite.
subdomain1.domain.com should render as domain.com/subdomain1 in the address bar

Change in address bar = full-fledged redirect.

You can do it in a single RewriteRule if everything follows the same formula. You would then have a RewriteCond saying, in part,

%{HTTP_HOST} ^(sub1|sub2|sub3)\.example\.com

... and let's see you do the second step :) Come back when you've got that far. List the subdomains by name in the Condition; seven isn't enough for a wild-card approach where you'd need a separate function to pick up the typos and misspellings.

jackx



 
Msg#: 4567684 posted 2:58 pm on Apr 25, 2013 (gmt 0)

Hi Lucy - thanks for the clarification, etc. - yes, this is a full on redirect.

Also, thanks for the first step, greatly appreciated - was hoping for a compete example? (much easier to learn from in my case - deconstructing what works rather than trying to write what I don't know)

That said, I'll continue trying to work from what you've provided.

Also, I'm curious, what would a wildcard approach look like?

Dideved



 
Msg#: 4567684 posted 5:22 pm on Apr 25, 2013 (gmt 0)

Here's one way it could work (untested):

# the rewrite rule below will be applied only if this condition passes
# this condition tests if the user came in through one of the subdomains
# it will also remember the subdomain value (using parentheses)
RewriteCond %{HTTP_HOST} ^(subdomain1|subdomain2|subdomain3)\.example\.com$

# the subdomain value we captured from the condition can be used in the rewrite substitution as %1
# and the path value we capture from the rewrite pattern below can be used as $1
# by default, this would issue a temporary (302) redirect
# the [R] flag lets us change this to a permanent (301) redirect
RewriteRule (.*) http://example.com/%1/$1 [R=permanent]


The wild-card approach means you have the option to capture any subdomain, whatever its name may be. On the plus side, your rewrite code would be shorter and simpler. On the downside, it may redirect some subdomains that you didn't intend. So make sure you consider all the scenarios if you're going to try this. Here's an example how it could work:

RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$
jackx



 
Msg#: 4567684 posted 5:53 pm on Apr 25, 2013 (gmt 0)

In re-reading my post, I want to make sure that I was clear in what I need to resolve:

I have a set of subdomains that need to render in the browser as a subfolder structure, even though the content of each does not reside in these subfolders.

Example: subdomain1.domain.com should render as domain.com/subdomain1

I am reading about the Flag 'P' - P (proxy - i.e., apache should grab the remote content specified in the substitution section and return it)

The examples I've been provided (thanks a bunch!) do not include that flag, so I wanted to make sure that I have been as clear as possible so I can resolve this (as quickly as possible)

Many thanks in advance -

Dideved



 
Msg#: 4567684 posted 6:28 pm on Apr 25, 2013 (gmt 0)

> ... even though the content of each [subdomain] does not reside in
> these subfolders.

That is indeed a crucial detail. So where then does the subdomain content reside?

jackx



 
Msg#: 4567684 posted 10:04 pm on Apr 25, 2013 (gmt 0)

at the subdomain level

Dideved



 
Msg#: 4567684 posted 10:07 pm on Apr 25, 2013 (gmt 0)

So... you want your subdomains to redirect to the main domain (to make the address bar look pretty), then you want the main domain to proxy back to the subdomains for content. Does that sound about right?

lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4567684 posted 10:08 pm on Apr 25, 2013 (gmt 0)

I have a set of subdomains that need to render in the browser as a subfolder structure, even though the content of each does not reside in these subfolders.

That's the beauty of a rewrite. Content of browser's address bar may or may not have anything to do with where the material is to be found. Typically you'll have two pieces:

redirect to what you want browser's address bar to say
followed by
rewrite to where the content really lives

If the original request happens to be the same as the material's "real" location, the redirect has to come with a RewriteCond looking at THE_REQUEST so you don't go around in circles. Happily this doesn't apply here, because a subdomain is never a "real" location. (On account of how servers don't operate in four dimensions.)

Matter of fact I don't get where the subdomains enter into the picture at all. If you want the user's address bar to change, then we are not talking about the vanilla subdomain rewrite where address bar says
subdomain.example.com/morestuff
while their browser serves up material from
www.example.com/subdomain/morestuff

What's the role of a subdomain in all this? I hope your site isn't entirely built on redirects, where you publish URLs using subdomains but then redirect everyone who comes in asking for the URL you told them to ask for.

And, for that matter: Where does the content reside? I don't think you ever said.

About wild cards: Your rewrite structure would say something like (don't cut-and-paste, this is off the top of my head)

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

or [P] flag (only) depending on whether you wanted a redirect or a behind-the-scenes rewrite. This version doesn't check whether the %1 element-- the subdomain-- actually exists, so you'd need some supplementary code to deal with requests for nonexistent subdomains. Or accept the risk of 301-to-404 sequences. This pattern apparently makes search engines grumpy-- but if they only get there by asking for things that they invented out of thin air, they've really got no business complaining.

jackx



 
Msg#: 4567684 posted 10:49 pm on Apr 25, 2013 (gmt 0)

@dideved - yes, I want the subdoamains to redirect to the main domain, in a subfolder structure, sub1.domain.com to domain.com/sub1 and proxy back to the subdomain for the content

@lucy24 - still digesting all that you've posted, however unfortunately yes, there are some legacy pieces of this puzzle where redirects are being used as you've described (which I did not set up)

jackx



 
Msg#: 4567684 posted 11:04 pm on Apr 25, 2013 (gmt 0)

@lucy24 - what I am trying to solve is the opposite of your example

address bar says
subdomain.example.com/morestuff
while their browser serves up material from
www.example.com/subdomain/morestuff

Instead:
address bar says
example.com/sudomain1
while their browser serves up material from
subdomain1.example.com/

I want the subfolder in the above example to match the subdomain name, ideally by reading the name of the subdomain and then displaying it as the subfolder

Example: subdomain1.example.com redirects to example.com/subdomain1

lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4567684 posted 6:55 am on Apr 26, 2013 (gmt 0)

Uhmm... so you want the opposite of what you said in your first post?
subdomain1.domain.com should render as domain.com/subdomain1 in the address bar

To me that looks like a redirect. Unless "should render as ... in the address bar" really means "should display content from ... BUT NOT reflect this in the address bar." You can see where there's a potential touch of ambiguity, right?

:: detour to reread posts ::

:: pause for head to stop spinning ::

We're going to need a few more unambiguous "This is what I WANT to have happen" vs. "This is what CURRENTLY happens but I don't want it to".

address bar says
subdomain.example.com/morestuff
while their browser serves up material from
www.example.com/subdomain/morestuff

Instead:
address bar says
example.com/sudomain1
while their browser serves up material from
subdomain1.example.com/

One of the above is what you want. The other is what you don't want. Which is which?

their browser serves up material from
subdomain1.example.com

This is flat-out impossible, because "subdomain.example.com" has no physical existence. The only way you can serve content in response to a request for subdomain.example.com is if your server has been told where the said content lives. Typically this is a directory within the example.com directory-- but the server doesn't know this unless it has already been told.

Come to think of it, where is all this happening? I normally assume shared hosting unless the post says very clearly and unambiguously that it's your own server.

If it is shared hosting, the DNS has presumably been handled by the host, and all you have to deal with is flags. A redirect ends with the flag [R=301,L] while a rewrite ends in [L] alone. The [P] part is then a red herring. If this is all happening in the same userspace on the same server, the request isn't going anywhere and you don't need to mess with proxies. In fact I don't see any need for proxies regardless of whose server it is, so long as it's all the same one.

Dideved



 
Msg#: 4567684 posted 9:57 am on Apr 26, 2013 (gmt 0)

The proxy is actually the easy part. This is all that the main domain htaccess should need.

# main domain htaccess

# any request for example.com/subdomain/* should be fulfilled
# by another server located at subdomain.example.com
RewriteRule ^(subdomain1|subdomain2|subdomain3)(?:/(.*))?$ http://$1.example.com/$2 [P]


But now, to have the subdomain redirect back to the main domain becomes complicated, because you'll end up in a redirect loop (the subdomain redirects to the main domain, which proxies to the subdomain, which redirects to the main domain, and so on). To avoid that loop, we need to make sure that we *don't* redirect if the request is a proxy request from the main domain.

# subdomain htaccess

# the redirect below will be applied only if this condition passes
# this condition tests if the current request is *not* a proxy request from the main domain
RewriteCond %{HTTP:X-Forwarded-Host} !^example.com$

# this additional condition may be redundant
# it's useful if the main domain and subdomains share the same htaccess file
# that doesn't sound like it's the case here
# yet we still need this condition so that we can capture the subdomain value
RewriteCond %{HTTP_HOST} ^([^.]+)\.example.com$

# the subdomain value we captured from the condition can be used in the rewrite substitution as %1
# and the path value we capture from the rewrite pattern below can be used as $1
# by default, this would issue a temporary (302) redirect
# the [R] flag lets us change this to a permanent (301) redirect
RewriteRule (.*) http://example.com/%1/$1 [R=permanent]

jackx



 
Msg#: 4567684 posted 6:33 pm on Apr 29, 2013 (gmt 0)

@dideved - thank you, am making progress here, based upon your examples.

Which of the conditions / rules would I use when I am going to modify the httpd.conf file within Apache.

Your assumption that I am doing this via .htaccess file is correct, however I will eventually want to implement this on Apache

Thanks in advance!

lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4567684 posted 9:11 pm on Apr 29, 2013 (gmt 0)

I will eventually want to implement this on Apache

You mean in the server's config file, right? htaccess is just as much Apache as the config file is; it's simply got a narrower scope. Don't forget to add/remove leading slashes as needed-- and keep track of opening anchors and subdirectory names, especially if the htaccess was in a deeper directory.

Only the pattern needs to change. Conditions will remain the same either way.

jackx



 
Msg#: 4567684 posted 9:24 pm on Apr 29, 2013 (gmt 0)

@lucy24 - yep, in the servers config file.

I have seen reference to leading slashes, but nothing too clear.

What would the following turn into (taken from @dideved's example

RewriteRule ^(subdomain1|subdomain2|subdomain3)(?:/(.*))?$ http://$1.example.com/$2 [P]

[edited by: phranque at 12:02 am (utc) on Apr 30, 2013]
[edit reason] unlinked example.com url [/edit]

Dideved



 
Msg#: 4567684 posted 9:28 pm on Apr 29, 2013 (gmt 0)

I think everything can stay the same provided that it's put into a Directory directive.

# main configuration file
<Directory "/path/to/dir/where/htaccess/used/to/be">
# everything that was in htaccess goes here instead
</Directory>

lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4567684 posted 11:27 pm on Apr 29, 2013 (gmt 0)

I have seen reference to leading slashes, but nothing too clear.

In this respect, htaccess and <Directory> sections behave the same. It's what the apache docs like to call "per-directory context".

According to the horse's mouth [httpd.apache.org] (link is to 2.2 but 2.4 is identical):
The removed prefix always ends with a slash, meaning the matching occurs against a string which never has a leading slash. Therefore, a Pattern with ^/ never matches in per-directory context.

For a given definition of "never", that is. A malformed URL might have duplicate directory slashes, which could then conceivably occur at the beginning of a pattern. But let's not split hairs.

:: noting with horror that for some time now, I have been doing something which that selfsame horse's mouth says "should never be necessary and is unsupported". I am by no means the first person in history to do something because I didn't know you couldn't. ;) ::

jackx



 
Msg#: 4567684 posted 5:45 pm on May 2, 2013 (gmt 0)

Thank you both for your help - greatly appreciated

phranque

WebmasterWorld Administrator phranque us a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



 
Msg#: 4567684 posted 7:12 am on May 3, 2013 (gmt 0)

welcome to WebmasterWorld, jackx!

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