SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Mar 2013
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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/su...e/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/su...e/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

  2. #2
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,672
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    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 [code]...[/code] tags as that preserves the code when quoting for a reply):

    Code:
    RewriteCond %{HTTP_HOST} ^subdomainthree\.example\.com [NC]
    # Don't forget to escape the dot characters in your regular expressions!
    RewriteRule ^/(.*) http://subdomainone.example.com:8010/$1 [L,R,proxy]
    # 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.
    # 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.
    
    RewriteCond %{HTTP_HOST} ^subdomainthree.example.com [NC]
    RewriteRule ^/(.*) http://subdomaintwo.example.com:8092/subdomainthree [L,R,proxy].example.com/subdomainthree [NC]
    # No case in a RewriteRule (for {REQUEST_URI}) is just dead wrong! Remember, {REQUEST_URI} IS case sensitive!
    
    RewriteRule ^/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 ^/(.*) http://subdomaintwo.example.com:8092/subdomainthree [L,R,proxy]
    [standard rant #1]
    The use of "lazy regex," specifically the EVERYTHING 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!
    [/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
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  3. #3
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,311
    Mentioned
    19 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by mmulq View Post
    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

    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]
    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]
    "First make it work. Then make it better."

  4. #4
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,311
    Mentioned
    19 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by dklynn View Post
    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!
    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.
    "First make it work. Then make it better."


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •