pyrsmk pyrsmk - 4 months ago 43
Apache Configuration Question

Apache URL rewriting with a maintenance page

I'm not really used to Apache rewrite module, so I need a little help ^^

I currently have this :

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !\.(js|ico|gif|jpe?g|png|css|html|xml)$ index.php [QSA,L]

It redirects all requests to
if they're not pointing to an existing file with the extension listed above.

Now, I want to add a rule that says :

  • if the file maintenance.html exists at root, redirects to it for any request

  • otherwise send the request to index.php (or return the file if it's an existing and allowed file)

Thanks :)


I'd say you just have to add the exception before the usual rewriting:

RewriteEngine On

RewriteCond /maintenance.html -f
RewriteRule ^ maintenance.html [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !\.(js|ico|gif|jpe?g|png|css|html|xml)$ index.php [QSA,L]

And a general note. You should always prefer to place such rules in the actual http servers host configuration. .htaccess style files are notoriously error prone, are hard to debug and they really slow the server down, often without reason. They are only offered for situations where you do not have access to the host configuration (read: really cheap hosting providers) or if an application needs to write its own rewrite rules (which is a security nightmare anyway).