Posts Tagged ‘mod_rewrite’

Because my brain is a sieve here’s how to serve custom maintenance pages based on the subdomain

Tuesday, May 25th, 2010

Just like the favicons post of yore, this is another one that came up in conversation, “Can we display a custom maintenance page for each subdomain?” Since I had managed to sketch out a proof-of-concept with the favicons I figured this one shouldn’t be too hard.

The way this works is that Apache checks for a file called maintenance.on and if it exists it rewrites all traffic to maintenance-[subdomain].html. In practice, maintenance.on is set during deploys that require downtime such as one that might do work on the database, and at the end of a successful deploy the file is either removed or moved to something like maintenance.off.

Granted, this is a little kludgy as it depends on a separate html file for each subdomain and I don’t have a fallback position if that maintenance file doesn’t exist, Apache will just serve up a 404.

Anyway, here’s what’s in the Apache site file:

# Check for the maintenance.on file and redirect all requests to
# to a custom maintenance page based on the following formula
# maintenance-[subdomain].html
# get the requested subdomain name into variable %1
RewriteCond %{HTTP_HOST} ^([^.]+)\.*


# skip the rules for images and css
RewriteCond %{REQUEST_URI} !\.(png|jpg|jpeg|gif|css|ico)$


# check to see if maintenance.on exists
RewriteCond %{DOCUMENT_ROOT}/system/maintenance.on -f
RewriteCond %{SCRIPT_FILENAME} !maintenance-1%.html


# write out the custom maintenance page based on the domain
RewriteRule ^.*$ /system/maintenance-%1.html [L]

This was gratefully stolen and extended from the Codahale post, Time For A Grown-Up Server: Rails, Mongrel, Apache, Capistrano and You.

Because my brain is a sieve here’s how to serve custom favicons based on the subdomain.

Thursday, January 21st, 2010

The ability to serve, from the same code base, custom favicons based on subdomains came up recently and because my mod_write fu is about as strong as a water soaked kleenex I’m documenting just what the hell I did to get it working as a proof-of-concept.


RewriteEngine on
# This is just for troubleshooting purposes, comment it out
RewriteLog /var/log/apache2/rewrite.log
RewriteLogLevel 9


# Rewrite index.html to %2
RewriteRule ^/index.html$ /%2.html


# If the request is not for the the main domain or the www subdomain
RewriteCond %{HTTP_HOST} !^(www\.)?megafuntimefor\.us


# get the requested subdomain name into variable %2
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.megafuntimefor\.us


RewriteRule ^/images/favicon.ico /images/%2.ico

Above is just the mod_rewrite block and while I’m sure that their is a drier way to write it I have yet to attain that level of enlightenment.

Using mod_rewrite to force SSL on directories

Tuesday, December 30th, 2008

Not that this is exceptionally difficult but I really needed to post this just so I would have a reference in the future.

<VirtualHost *:80>
ServerName your.server.here
ServerAlias your.server.here

RewriteEngine on
RewriteCond  %{REQUEST_URI} (private|secret|goaway)
RewriteRule ^(.*)$ https://%{SERVER_NAME}$1 [L,R]

# The rest of the configuration follows…
</VirtualHost>

<VirtualHost *:443>
ServerName your.server.here
ServerAlias your.server.here

RewriteEngine on
RewriteCond  %{REQUEST_URI} !(private|secret|goaway)
RewriteRule ^(.*)$ http://%{SERVER_NAME}$1 [L,R]

# The rest of the configuration follows…
</VirtualHost>

Essentially any requests for private, secret, or goaway will get punted to https while all others will be handle as non-SSL.  In the 443 block, to make sure that you aren’t using SSL for assets that don’t require it we check again this time to see if it is not the listed directories and punt them back to non-SSL. This can also be used in htaccess.