.htaccess tips and tricks

June 19th, 2011 by Tony de Jesus

.htaccess is a file used in Apache Web Server that allows us to make configuration changes on a per-directory basis. It’s very useful when you need, for example, to redirect your users to a different page or block certain IP addresses to access your website (or a specific folder). This article shows some basic and useful .htaccess directives, to get you started with .htaccess immediately.

Allow directory browsing

If you don’t include an index file in a directory, most servers are configured to display a “Forbidden” error message. However, sometimes you need to list all files and folders contained in the directory. To do this, you only need to add the following line to your .htaccess file:


Options All +Indexes

 

Prevent directory browsing

Sometimes, servers allow you to browse a directory if you don’t include an index file on it. But you can prevent that by adding a single line to your .htaccess file:


Options All -Indexes

 

Set the default page of each directory

If for some reason you don’t want to use an index page in each directory, you can change the default page visited when someone reaches that directory by adding the following directive:


DirectoryIndex filename.html

(You will have to replace the “filename.html” section with whatever you want to use as the default.)

 

Customize error pages

If you want to create a custom error page that will be displayed to your visitors each time a specific error occurs, you only need to specify the error code and the file to display. Please check the following examples:


#400 Bad Request

ErrorDocument 400 /400.html

#401 Unauthorized

ErrorDocument 401 /401.html

#403 Forbidden

ErrorDocument 403 /403.html

#404 Not Found

ErrorDocument 404 /404.html

 

Redirect to another page (301 redirect)

If for some reason you need to change the structure of your website and need to redirect some old URLs to their new locations, you only need to add a line of code like this for each URL:


Redirect 301 /oldstructure/filename.html http://domain.com/newstructure/filename.html

Redirect 301 /oldstructure/filename2.html http://domain.com/newstructure/filename2.html

 

Redirect non-www to www

Sometimes you need that your users always access your website using the ‘www’ part in the URL. The following lines of code will add ‘www’ to any non-www URL:


RewriteEngine on

RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]

RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

 

Redirect www to non-www

Same as previous one but instead of adding ‘www’, it will remove the ‘www’ part of any URL.


RewriteEngine on

RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]

RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

 

Change PHP configuration parameters

If you need to change some PHP configuration parameters and you don’t have access to the server’s php.ini file, you can do it by using the htaccess file:

#Format

php_value setting_name setting_value

#Examples

php_value upload_max_filesize 10M
php_value post_max_size 20M
php_value memory_limit 128M

 

Block visitors by IP

Is there a person posting spam comments every day on your website? So it’s simple: block him! Just use the following lines of code:


<Limit GET POST>
order allow,deny
allow from all
deny from 123.456.789.321
deny from 24.112.106.235
</Limit>

Of course, you need to change the IP Addresses in order to suit your needs.

 

Allow visitors by IP

Are you developing a new website that is still in the tests phase and you don’t need anyone else than you (and/or your team) to access it? This is also simple: add the following lines of code, replacing the IP Addresses by the ones that you want to grant access privileges:


<Limit GET POST>

order deny,allow
deny from all
allow from 123.456.789.321
allow from 24.112.106.235

</Limit>

So, what do you think? Do you know any other useful .htaccess directives that you want to share? Then please use the comments section.

 
 
 

Leave a Reply