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>
@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