Redirecting subdomain to subdirectory

Hello,

I recently inherited an Apache web server when I accepted a position in a university lab. The job was only supposed to involve being the sys admin for a couple of high performance clusters but now it seems I need to learn a bit about mod_rewrite. The Apache server listens on port 192.168.1.93:80 and there is a python web server that listens on 192.168.1.97:8092. In our DNS server, subdomainone.example.com points to 192.168.1.93 and subdomaintwo.example.com points to 192.168.1.97. Another python web server used to listen on 192.168.1.93:8010 but that server was folded into the one on 192.168.1.97:8092 and is available at 192.168.1.97:8092/subdomainthree. There is a rule in place that used to redirect requests for subdomainthree.example.com to subdomainone.example.com:8010 but I’ve been asked to amend the rules to make subdomainthree.example.com redirect to subdomaintwo.example.com:8092/subdomainthree. I’m fairly sure it can be done but I’ve gone through a few regular expression tutorials and have yet to come up with a solution that works.

Here is the old rule for subdomainthree.example.com:

RewriteCond %{HTTP_HOST} ^subdomainthree.example.com [NC]
RewriteRule ^/(.*) http://subdomainone.example.com:8010/$1 [L,R,proxy]

This is my most recent attempt to get it working with the current configuration:

RewriteCond %{HTTP_HOST} ^subdomainthree.example.com/subdomainthree [NC]
RewriteRule ^/docking/(.*) http://subdomaintwo.example.com:8092/subdomainthree/$1 [L,R,proxy]

RewriteCond %{HTTP_HOST} ^subdomainthree.example.com [NC]
RewriteRule ^/(.*) http://subdomaintwo.example.com:8092/subdomainthree [L,R,proxy]

The initial redirect works but, when I try to follow links to any subdirectories from there, I get the same page with a URL that looks like this:

http://subdomainthree.example.com/subdomainthree/subdirectory

Clicking on the link to the same subdirectory again gives me the same page again with the following URL displayed:

http://subdomainthree.example.com/subdomainthree/subdomainthree/subdirectory

My understanding of regular expressions is pretty poor as I’m sure is evident by this attempt. Could someone help me to understand what is going on here and help me correct it?

Thanks,

Matt

mm,

First and foremost, WELCOME to SitePoint!

Then, please allow me to add to your list of mod_rewrite tutorials: You might benefit from reading the mod_rewrite tutorial linked in my signature as it contains explanations and sample code. It’s helped may members and should help you, too.

Finally, it would appear that the Apache variable you’re looking for is {SERVER_PORT} to enable you to distinguish what port is being used/requested.

As for your code (PLEASE wrap your code in [noparse]

...

[/noparse] tags as that preserves the code when quoting for a reply):

RewriteCond %{HTTP_HOST} ^subdomainthree[COLOR="#0000FF"][SIZE=4]\\[/SIZE][/COLOR].example[COLOR="#0000FF"][SIZE=4]\\[/SIZE][/COLOR].com [NC]
[COLOR="#0000FF"]# Don't forget to escape the dot characters in your regular expressions![/COLOR]
RewriteRule ^[COLOR="#FF0000"]/[/COLOR](.*) http://subdomainone.example.com:8010[COLOR="#800080"]/$1[/COLOR] [L,R,[COLOR="#FF8C00"]proxy[/COLOR]]
[COLOR="#FF0000"]# The leading / after the ^ infers (DEMANDS) use of Apache 1.x and there are very few left "in the wild"
# It will NOT be matched by Apache 2.x!
# If you're not sure (as a sysadmin!?!), use ^/? but use the correct code for the server if you can!
# Since ^(.*)$ already exists as {REQUEST_URI}, use RewriteRule .? http://subdomainone.example.com:8010%{REQUEST_URI}
#    because it also resolves your ^/? issue painlessly.[/COLOR]
[COLOR="#FF8C00"]# proxy?
# Apache.org: Use this flag to achieve a more powerful implementation of the ProxyPass directive, to map remote content into the namespace of the local server.
# Apache.org: Use of the [proxy|P] flag implies [L] - that is, the request is immediately pushed through the proxy, and any following rules will not be considered.
# Apache.org: Note: mod_proxy must be enabled in order to use this flag.[/COLOR]

RewriteCond %{HTTP_HOST} ^subdomainthree.example.com [NC]
RewriteRule ^[COLOR="#FF0000"]/[/COLOR](.*) http://subdomaintwo.example.com:8092/subdomainthree [L,R,proxy].example.com/subdomainthree [[COLOR="#FF0000"]NC[/COLOR]]
# No case in a RewriteRule (for {REQUEST_URI}) is just dead wrong! Remember, {REQUEST_URI} IS case sensitive!

RewriteRule ^[COLOR="#FF0000"]/[/COLOR]docking/(.*) http://subdomaintwo.example.com:8092/subdomainthree/$1 [L,R,proxy]
# What domain does this apply to? Hint: It does not use the above RewriteCond statement (which belongs to the RewriteRule above).

RewriteCond %{HTTP_HOST} ^subdomainthree.example.com [NC]
RewriteRule ^[COLOR="#FF0000"]/[/COLOR](.*) http://subdomaintwo.example.com:8092/subdomainthree [L,R,proxy]

[standard rant #1][indent]The use of “lazy regex,” specifically the :kaioken: EVERYTHING :kaioken: atom, (.*), and its close relatives, is the NUMBER ONE coding error of newbies BECAUSE it is “greedy.” Unless you provide an “exit” from your redirection, you will ALWAYS end up in a loop![/indent][/standard rant #1]

Hmmm, no examination of the {SERVER_PORT} so, apparently, you’re not checking on the domain in use via the port requested. Your explanation was probably pretty exact (domains and ports going every which way) but I concentrated on your mod_rewrite review request this post.

Please remember that showing only a portion will leave conflicts untouched if they exist outside what you’ve shown.

Regards,

DK

Seems like the minimum changes you should need to make to the old rule are (highlighted in red):

RewriteCond %{HTTP_HOST} ^subdomainthree.example.com [NC]
RewriteRule ^/(.*) http://subdomaintwo.example.com:8092/subdomainthree/$1 [L,R,proxy]

FYI. I downloaded Apache 1.3.41 and tested this claim. It turns out that both Apache 1 and Apache 2 behave the same way regarding the leading slash. Here’s how it works in both versions of Apache:

If the rewrite rule is in a per-directory context – either an htaccess file or inside a <Directory> section – then there must not be a leading slash. But if the rewrite rule is in a per-server context, then there must be a leading slash.