Skip to content

Instantly share code, notes, and snippets.

@jadell
Created December 17, 2010 14:02
Show Gist options
  • Save jadell/744971 to your computer and use it in GitHub Desktop.
Save jadell/744971 to your computer and use it in GitHub Desktop.
Dynamic Subdomains in Apache
RewriteEngine on
# look for URLS like 'subdir.username.foo.bar'
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} ([^.]+)\.([^.]+)\.foo\.bar$
RewriteCond /home/%2/public_html/%1 -d
RewriteRule ^(.+) %{HTTP_HOST}$1 [C]
RewriteRule ([^.]+)\.([^.]+)\.foo\.bar(?::\d+)?(.*) /home/$2/public_html/$1$3 [L]
# look for URLS liks 'username.foo.bar'
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} ([^.]+)\.foo\.bar$
RewriteCond /home/%1/public_html -d
RewriteRule ^(.+) %{HTTP_HOST}$1 [C]
RewriteRule ([^.]+)\.foo\.bar(?::\d+)?(.*) /home/$1/public_html$2 [L]
# On a server that doesn't have the userdir module and it's associated configs, we also need to add this:
<Directory /home/*/public_html>
AllowOverride FileInfo AuthConfig Limit Indexes
Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec
<Limit GET POST OPTIONS>
Order allow,deny
Allow from all
</Limit>
<LimitExcept GET POST OPTIONS>
Order deny,allow
Deny from all
</LimitExcept>
</Directory>
# Also, if the userdir module is enabled and you want to use PHP in userdirs,
# Ubuntu require the additional step of modifying /etc/apache2/mods-available/php5.conf:
<IfModule mod_php5.c>
<FilesMatch "\.ph(p3?|tml)$">
SetHandler application/x-httpd-php
</FilesMatch>
<FilesMatch "\.phps$">
SetHandler application/x-httpd-php-source
</FilesMatch>
# To re-enable php in user directories comment the following lines
# (from <IfModule ...> to </IfModule>.) Do NOT set it to On as it
# prevents .htaccess files from disabling it.
# <IfModule mod_userdir.c>
# <Directory /home/*/public_html>
# php_admin_value engine Off
# </Directory>
# </IfModule>
</IfModule>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment