Skip to content

Instantly share code, notes, and snippets.

@jrschumacher
Created January 11, 2012 09:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jrschumacher/1593837 to your computer and use it in GitHub Desktop.
Save jrschumacher/1593837 to your computer and use it in GitHub Desktop.
Wildcard domain for local development using Apache Mod Rewrite
#!/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>
@koke
Copy link

koke commented Jan 11, 2012

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

@jrschumacher
Copy link
Author

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

@jaywilliams
Copy link

I might have to try this myself, as I'm always adding new VirtualHosts for projects.

@jrschumacher
Copy link
Author

Change Log:

Added redirect for missing favicons so as to not pollute the log.

@jrschumacher
Copy link
Author

Change Log:

Added a beginning slash due to issues with some javascript files not loading.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment