.htaccess Tips





.htaccess



The Swiss army knife that is Apache

Easily create your Apache configuration file and set permission rules for directories of your site, create redirects and customise error pages.





Protect your folders with passwords


With a .htaccess file, you can protect parts of your site with a password.







Filter the IPs


You can prohibit access to certain users based on their IP addresses. Very useful for giving access to one part to allow work for developers.

Example:

Deny from all Allow from 192.168.xxx.xxx





Manage cookies

The .htaccess allows you perform cookie operations of your visitors. You can set cookies, change it, retrieve values and apply redirects.


Example:

Redirect if the cookie is not created

RewriteEngine On RewriteBase / RewriteCond %{HTTP_COOKIE} !^.*cookie-name.*$ [NC] RewriteRule .* /login-error/set-cookie-first.cgi [NC,L]




Rewrite complex URLs

Rewrite a complex url so that it is more easily read by visitors.



Example:

# Enabling a URL rewrite module: RewriteEngine on # Rules of URL rewrites : RewriteRule ^calendrier-(\w+)-(\d+)-(\d+)-(\d+).html$ /calendrier.php?user=$1&day=$2&month=$3&year=$4




Personalise the default error pages:

Replace the 404,401,403,500 default pages... with pages designed like your website.


Example:

ErrorDocument 401 /autorisation_requise.html ErrorDocument 400 /mauvaise_requete.html ErrorDocument 403 /interdit.html ErrorDocument 404 /page_inexistante.html




Manage redirections:

Guide the visitors to your site with temporary or permanent redirections.


Example:

RedirectPermanent /photos/vacances-canada.html /canada.html




Configure your environment:

Choose the php version to use and configure register_global, zend optimiser, ioncube and magic quotes ...


Example:

Use php4

AddHandler application/x-httpd-php4 .php .php4 .php3