-
-
Save jrschumacher/1593837 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl | |
### | |
# Sub2Path is a little script which convert a url with subdomains to a path | |
# | |
# To be used with Apache ModRewrite RewriteMap found: http://goo.gl/8WUvm | |
# Follow the conversation at: http://goo.gl/Mzc5E | |
# Props to 42foo.com: http://goo.gl/mut77 | |
# | |
# MIT license | |
## | |
$| = 1; # Turn off I/O buffering | |
while (<STDIN>) { | |
s/(^|\.)[^\.]+\.[^\.\s]+(\s*)$//g; # Remove tail (ie. go.dev) | |
my @path = reverse(split(/\./, $_)); # Reverse order of subdomain | |
print '/' . join('/', @path) . '/' . $2; # Join with / | |
} |
# Only if not already defined | |
NameVirtualHost *:80 | |
RewriteLock /tmp/apache2_lock | |
<VirtualHost *:80> | |
ServerName 42foo.com | |
ServerAlias *.42foo.com | |
ServerAdmin webmaster@42foo.com | |
DocumentRoot /Users/Ryan/Projects | |
LogFormat "%h %l %u %t \"%r\" %>s %b" common | |
CustomLog /opt/local/apache2/logs/access_log common | |
<Directory /Users/Ryan/Projects/> | |
Options Indexes FollowSymLinks | |
AllowOverride AuthConfig FileInfo | |
Order allow,deny | |
Allow from all | |
</Directory> | |
RewriteEngine on | |
# Handle missing favicons | |
Redirect 404 /favicon.ico | |
<Location /favicon.ico> | |
ErrorDocument 404 "No favicon" | |
</Location> | |
RewriteMap sub2path prg:/opt/local/apache2/conf/extra-available/sub2path.pl | |
RewriteCond %{HTTP_HOST} 42foo\.com$ | |
# Not sure if this is needed | |
# RewriteCond ${sub2path:%{HTTP_HOST}} -d | |
RewriteRule ^(.*) /${sub2path:%{HTTP_HOST}}/$1 [L] | |
</VirtualHost> |
So instead of having to define multiple <VirtualHost> for each project I wanted a way to use dynamically have subdomains. I found a reference (see conversation link) for doing this, but it only handled one directory deep. So every project file would have to be either under one directory. What this does is is map the url to the dir:
url: experimental.someproject.somecompany.42foo.com
dir: /Users/Ryan/Projects/somecompany/someproject/experimental
url: release.anotherproject.anothercompany.42foo.com
dir: /Users/Ryan/Projects/anothercompany/anotherproject/release
I might have to try this myself, as I'm always adding new VirtualHosts for projects.
Change Log:
Added redirect for missing favicons so as to not pollute the log.
Change Log:
Added a beginning slash due to issues with some javascript files not loading.
Not sure if I follow, but if it does what I think it does, you can use VirtualDocumentRoot for that :) http://httpd.apache.org/docs/2.0/vhosts/mass.html