Posts about Apache server.

Apache HTTP Logo

When To Use .htaccess Files

1st May 2008 - 3 minutes read time

Hypertext access, or .htaccess files, allow you to change the Apache configuration on a by directory basis. However, you should always use the main server configuration file to do configuration changes whenever possible. This is because when Apache is configured to process .htaccess files it looks at every directory underneath the current directory to see if there are any files present, resulting in a slightly longer page load time. Although this might not be noticeable with low traffic levels, at high traffic levels it can cause sites to slow down. You should therefore use .htaccess files only when the main server configuration file (http.conf) is inaccessible.

To increase performance you can use the AllowOverride directive in your top level directory, or any directory who's subdirectories do not use .htaccess files. This will stop Apache from searching through all sub directories.

Read the full article

Apache HTTP Logo

Preventing Image Bandwidth Theft With .htaccess

21st April 2008 - 4 minutes read time

When people link to your images from their own site they are essentially using your bandwidth to show images on their site, this is also known as hotlinking.

The simplest way of preventing people from doing this is to add a .htaccess file to only allow locally linked images to be served. This checks the domain that is linking to your images by using the referrer and if the domain does not equal you own site then a different image is served, in this case blank.jpg.

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_REFERER} !^http://(.+\.)?hashbangcode\.com/ [NC]
RewriteCond %{HTTP_REFERER} !^http://(.+\.)?google\.co\.uk/ [NC]
RewriteCond %{HTTP_REFERER} !^http://(.+\.)?google\.com/ [NC]
RewriteRule .*\.(jpe?g|gif|bmp|png)$ /images/blank.jpg [L]

You can also prevent hotlinking from high traffic sites like myspace by using the following .htaccess file.

Read the full article

Apache HTTP Logo

Using mod_rewrite On Form Parameters

5th March 2008 - 6 minutes read time

Using mod_rewrite on websites is fairly straightforward and can create some lovely looking URL structures. Instead of having a URL that contains lots of odd looking parameters like this:


You can use a .htaccess file to rewrite the URL on the server side in order to shorten this to something like this:


In this occasion the value of parameter2 will always be value2 so we can just include that in the rewrite rule, which would look something like the following. $1 is a back-reference to the first parenthesized value matched in the RewriteRule.

Read the full article

Apache HTTP Logo

Apache Bench Tool

25th February 2008 - 5 minutes read time

The Apache Bench tool can be found in the bin directory of any standard instillation of the Apache HTTP server. It can be used to test the response times of any web server you want and can be useful if you want to stress test a mission critical server before it goes live.

To use the tool open a command prompt (or terminal), navigate the Apache bin folder and find the program ab, this is the Apache Bench tool. The simplest form of running the tool is to use a single URL. However, you must enter a full file name or the tool will give you an invalid URL error.

ab http://www.google.com/index.html

This gives the following output.

Read the full article

Apache HTTP Logo

Avoiding URL Canonicalisation With mod_rewrite And Apache

22nd February 2008 - 3 minutes read time

URL canonicalisation is where you have a website with different URLs outputting the same content. When search engine spiders see all this content that is the same they can get confused as to what page to display in search engine result pages. The following URLs, although they are different, actually produce the same content.


The way to solve this issue is to redirect any requests to a single page using mod_rewrite. Add a .htaccess file to your root directory and include the following line to turn on the engine.

RewriteEngine On

The following rule will redirect the www page to the non-www page.

Read the full article

Apache HTTP Logo

Setting Up LDAP With Active Directory On Apache

21st January 2008 - 5 minutes read time

Using a simple .htpasswd to password protect a directory or website is fine if you only have a few users, and they don't change very much. However, this quickly becomes impossible to maintain if you have lots of users. For example, if you wanted to secure access to the company Intranet you might spend quite some time trying to update your .htpasswd file. The best way to do this is to transfer all of the user administration over to an LDAP server and then get Apache to communicate with this directly. The Active Directory (AD) system that Microsoft uses allows LDAP communications, and as this is in use across many company networks it is an ideal candidate to use.

You first need to set of the LDAP modules on your Apache server. Uncomment or add the following lines in your http.conf file. You will need to make sure that the files actually exist as well.

Read the full article

Apache HTTP Logo

Setting php.ini Location In Apache

15th January 2008 - 3 minutes read time

After installing PHP on Apache you can use the php.ini file to set various different options to do with PHP. When Apache starts it uses what is contained in this file to set up and run PHP.

On both Windows, Unix and Linux systems Apache will look in a number of default locations for the php.ini file before giving up. You can explicitly tell Apache 2.x where to look for the file by using the PHPIniDir directive in the http.conf file.

# This is the directory containing php.ini
PHPIniDir "/usr/local/apache/conf"

For Apache 1.3.x this can be set using the SetEnv PHPRC directive.

# specify the directory where php.ini is
SetEnv PHPRC /usr/local/apache/conf

This not only speeds up the time taken for Apache to start, but will also allow you to make sure that the php.ini file you are using is the one you are editing.

Read the full article