Friday, January 28, 2011

.htaccess conditional application of rules and error redirection

Hi,

I'm looking to put a web application in production and I was wondering a few things about htaccess.

Here is my htaccess (the default htaccess provided by Zend Framework) :

SetEnv APPLICATION_ENV production

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

I would have liked to add two things in there, first a condition based on the APPLICATION_ENV to force SSL connection, but only in production (other possible values are development, testing and staging). The other thing I would have liked to set up is a redirection to the corresponding error action in the error controller based on the HTTP request error number.

I am not looking for a full answer, but at least where I need to start to be able to achieve my goals.

  • Here's a mod_rewrite cheatsheet and environment variable debugging HowTo for starters (nice supplement to the rather-sparse Apache mod_rewrite docs, IMHO).

    Your solution to the production/https task may look a bit like this:

    RewriteEngine on
    
    RewriteCond %{ENV:APPLICATION_ENV} ^production$
    RewriteCond %{HTTPS} !^on$
    RewriteRule .* https://%{HTTP_HOST}/$1 [R=301,L,QSA]
    
    Jeff : I have yet to try it on my Production server, but so far, with the little knowledge i have on htaccess, it makes sense
    From danlefree

0 comments:

Post a Comment