Skip to content

Instantly share code, notes, and snippets.

@lavoiesl
Created May 17, 2012 22:13
Show Gist options
  • Save lavoiesl/2721888 to your computer and use it in GitHub Desktop.
Save lavoiesl/2721888 to your computer and use it in GitHub Desktop.
VirtualHost Alias detection in .htaccess
# VirtualHost Alias detection in .htaccess
# https://gist.github.com/2721888
<IfModule mod_rewrite.c>
RewriteEngine On
# Neat trick to rewrite with or without leading slash
# Works by checking if DOCUMENT_ROOT + REQUEST_URI == REQUEST_FILENAME
# If not, it is because you are in a virtual host alias and DOCUMENT_ROOT is not reliable
# Example when alias is active:
# DOCUMENT_ROOT: /var/www
# real root: /var/www/example.com/
# REQUEST_FILENAME: /var/www/example.com/foo
#
# Requesting via alias:
# full request: http://example.com/foo
# REQUEST_URI: /foo
# test: /var/www + /foo == /var/www/example.com/foo => false
# Rewrites to: /app_dev.php
#
# Requesting via default host:
# full request: http://localhost/example.com/foo
# REQUEST_URI: /example.com/foo
# test: /var/www + /example.com/foo == /var/www/example.com/foo => true
# Rewrites to: /example.com/app_dev.php
<IfModule mod_vhost_alias.c>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}:::%{DOCUMENT_ROOT}%{REQUEST_URI} ^(.+):::\1
RewriteRule ^(.*)$ app_dev.php [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}:::%{DOCUMENT_ROOT}%{REQUEST_URI} !^(.+):::\1
RewriteRule ^(.*)$ /app_dev.php [QSA,L]
</IfModule>
# All this is useless if mod_vhost_alias is not active
<IfModule !mod_vhost_alias.c>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app_dev.php [QSA,L]
</IfModule>
# While the method above is nice, it is slow
# Be sure to deactivate all this when deploying, with or without RewriteBase
## RewriteBase /
# RewriteCond %{REQUEST_FILENAME} !-f
# RewriteRule ^(.*)$ app.php [QSA,L]
</IfModule>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment