Tag Archives: Apache

Put Your Website in Maintenance Mode With .htaccess

Sooner or later you’ll probably run into a case where you need to put up a “maintenance mode” page while working on the site (e.g. while moving a site to a different server). What’s the best way of doing that? First you create a small, static HTML page that will appear to your visitors, then you put this at the top of your .htaccess file:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/berightback\.html$
RewriteRule ^(.*)$ http://example.org/berightback.html [R=307,L]

If you still need to be able to access the live site while you perform the maintenance, you can add a condition permitting requests from your IP address. This line should be placed after the RewriteBase / line:

RewriteCond %{REMOTE_ADDR} !^00\.000\.000\.000

Be sure to replace the zeros in 00\.000\.000\.000 with the digits from your own IP address, which you can find by visiting WhatIsMyIP.com.

Get Live Access Logs With a Simple Linux Command

Here’s a neat little trick for those of you with SSH access to your Linux server. You can use a simple command to get a live-updated stream of your access log, so you can see hits as they come in. It’s sort of like the “spy” tool in Woopra or pMetrics, but geekier and with less visual polish. :)

Linux "tail -f" on an Apache Access Log

First you need to connect to the server with SSH. Once you’re in, change your working directory to wherever Apache stashes its log files on your system. On my Ubuntu install it’s /var/log/apache2. So I would type cd /var/log/apache2 to switch to that directory. Next, run the tail command like so:

tail -f access.log

Tail normally outputs the last several lines of a file and then returns to a prompt, but with the -f argument it continues to monitor the file for changes, and outputs them. So as the Apache server writes to the log file, tail spits the new lines out on your screen.

When you’re done, press Ctrl+C to terminate the tail process and go back to the shell prompt.

Saving Bandwidth and Speeding Up Your Site With GZIP and Browser Caching

There are a couple of easy adjustments you can make to your web server in order to decrease page loading times, save bandwidth, and reduce load on the server. All you have to do is add a couple of code snippets to either your Apache server configuration file (httpd.conf or apache2.conf) or an .htaccess file.

Note that these require that your server have certain modules installed for this to work. You will need either mod_deflate or mod_gzip for GZIP compression and mod_expires for the browser caching trick.

Enable Browser Caching

When a web browser loads a page, it checks each item it requests (JavaScript, CSS, images, etc) against its local cache. If an item, say the stylesheet, hasn’t expired yet, then it will load the local copy instead of requesting a new one. Now if you were to instruct your server to set the expiration time for images, CSS, and JavaScript files to one month from the present, users viewing multiple pages of your site (even across multiple days) won’t tax your resources as much, as they will use the copies of your stylesheets and images that have already been downloaded.

Continue reading →