Apache dynamic wildcard dynamic subdomain

I'm currently going to convert the old apache-vhost, where each one is created for each new version, to a dynamic host that can handle all subdomains, so we don't need to create a new time. I need help! We are using apache v2.2 .

purpose

Dynamic everything. To have one virtual host that handles all redirects for a specific set of subdomains. The URLs below, note that sub1

both branch

are dynamic and can 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 branch

as a subdirectory before the full url is the name of another subdirectory.

Also, /images/

the url needs to be forwarded shared-httpdocs/images

for each domain.

I still have vhost

<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 am trying to copy

This is an old ghost I'm trying to convert from. This is terrible, messy, and our operators have to create a new DNS record every time. What a joke! I need to figure 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 every time we have a new branch, so I am trying to mitigate this by providing a dynamic vhost subdomain (see the vhost I have). I was not even able to match /images/

in the url with a canned redirect loop.

How can I achieve my goal? I know this is a little tricky. If I can't do this, I just need to write a script that will generate a new vhost every time, but a dynamic one that “just works” would be fantastic. I've taken two days so far, I'm not an administrator. Your help would be greatly appreciated.

Resources I have used:

  • mod_rewrite official docs - shows the basics how things are conditional withREWRITE_COND

  • Subdomain rewriting - a question about subdomain rewriting
  • Asset Rewriting - Another question about rewriting things like images / css / js that doesn't seem to work for me.
+3


source to share


1 answer


This is not a complete answer, but too long to comment.

%0

The ( %0

to %9

) in the rewrite rule are the backreferences to the records in the last RewriteCond. I think you wanted the hostname instead. It also seems that you are missing a "branch" of part of the path. In Asset rewrite, you also strip off part of the filename.

# 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 the dedicated logging mod_rewrite with the RewriteLog and RewriteLogLevel directives.

Hope it brings you even more.

+2


source







All Articles