3

I'm currently in the process of converting an old apache vhost, where one is currently created for each new release, to a dynamic host that can handle all the subdomains so we don't have to create a new one all the time. I need some help! We're using apache v2.2.

The Goal

Dynamic all the things. To have a single virtual host that handles all redirects for a specific set of subdomains. The url is below, note that sub1 and branch are dynamic and could be anything.

sub1.branch.sandbox.domain.com

The directory structure for this is as follows:

/var/www/vhosts/branch/sub1.branch.sandbox.domain.com

As you can see above, the directory structure has the branch as a sub-directory before the full url is the name of another sub-directory.

Also, /images/ in the url needs to forward to shared-httpdocs/images for each domain.

The vhost I have so far

<VirtualHost *:80>
        ServerName branch.sandbox.domain.com
        ServerAlias *.branch.sandbox.domain.com

        VirtualDocumentRoot /var/www/vhosts/branch/%0

        Options Indexes FollowSymLinks

        # Rewrite Engine Stuff Here
        RewriteEngine On

        DirectoryIndex index.php

        # Assets needs to be forwarded - currently not working
        RewriteCond %{REQUEST_URI} ^/(css|js|images)/.*
        RewriteRule .*$ /var/www/vhosts/%0/shared-httpdocs/%1$ [L]

        # The HTTP dispatcher - currently not working
        RewriteCond %{HTTP_HOST} ^(.*)\.branch\.sandbox\.domain\.com [NC]
        RewriteRule ^(.*)$ /var/www/vhosts/%1/applications/portal/dispatchers/http.php [L,QSA,NS,NE,NC]
</VirtualHost>

The old host I'm trying to copy

This is the old vhost I'm trying to convert from. It's horrible, messy, and our ops has to create a new DNS entry every time. What a joke! I need to sort this out...

<VirtualHost *:80>
    # Notice how the stupid convention below will require a new DNS entry each time?
    ServerName sandbox.branch.domain.com
    ServerAlias sandbox.api.branch.domain.com

    DocumentRoot /var/www/vhosts/sandbox.branch.domain.com/applications/portal/httpdocs

    <Directory "/var/www/vhosts/sandbox.branch.domain.com/applications/portal/httpdocs">
            allow from all
            order allow,deny
            # Enables .htaccess files for this site
            #AllowOverride All

            RewriteEngine On

            # Rewrite all non-static requests to go through the webapp
            RewriteCond %{REQUEST_URI} ^/(css|js|images)/.*
            RewriteRule .* - [L]

            # Rewrite everything else to go through the webapp
            RewriteRule ^(.*)$ /dispatchers/http.php [QSA,L]

    </Directory>

    <Directory  "/var/www/vhosts/sandbox.branch.domain.com/applications/portal/dispatchers">
        allow from all
    </Directory>

    # Allow us to rewrite to the webapp without it being in the webroot
    Alias /dispatchers /var/www/vhosts/sandbox.branch.domain.com/applications/portal/dispatchers

    # Get shared/ to point to the shared static resources
    Alias /shared /var/www/vhosts/sandbox.branch.domain.com/shared-httpdocs

</VirtualHost>

A new DNS entry is required each time we have a new branch, so I'm trying to mitigate this by providing a dynamic subdomain vhost (see the vhost I have so far). I've gone from not even being able to match /images/ in the url to a permanent redirect loop.

How can I achieve my goal? I know it's a little complex. If I can't do it, I'll just have to write a script that will generate a new vhost each time but a dynamic one that 'just works' would be fantastic. I've put two days into this so far, I'm no sysadmin. Your help would be greatly appreciated.

Resources I have been using:

Community
  • 1
  • 1
Jimbo
  • 25,790
  • 15
  • 86
  • 131

1 Answers1

2

It's not a complete answer, but is too long for comment.

The %0 (%0 to %9) in a rewrite rule are back references to captures in the last RewriteCond. It seems to me you wanted instead the host name. Also it seems you miss the "branch" part of the path. In the Asset's rewrite you also throw away the filename part.

# Assets needs to be forwarded - currently not working
RewriteCond %{REQUEST_URI} ^/(css|js|images)/(.*)
RewriteRule .*$ /var/www/vhosts/branch/%{HTTP_HOST}/shared-httpdocs/%1/%2$ [L]


# The HTTP dispatcher - currently not working
RewriteCond %{HTTP_HOST} ^(.*)\.branch\.sandbox\.domain\.com [NC]
RewriteRule ^(.*)$ /var/www/vhosts/branch/%{HTTP_HOST}/applications/portal/dispatchers/http.php [L,QSA,NS,NE,NC]

You can get debugging help also from mod_rewrite dedicated logging with RewriteLog and RewriteLogLevel directives.

Hope this will bring you further.

Zimmi
  • 1,599
  • 1
  • 10
  • 14
  • This is helpful at least, I didn't know you could do a `%{HTTP_HOST}` for the variables within urls, so that's useful to know. +1 for that :-) – Jimbo Jul 28 '15 at 13:20