.htaccess Tips



The Swiss army knife that is Apache


Create your Apache configuration file and set permission rules for your site directories with ease, create redirects and customise error pages.





Protect your folders with passwords



Using an .htaccess file, you can password-protect specific parts of your website







Filter IPs


You can prohibit access to certain users based on their IP addresses. Very useful for providing access to a specific area for developers only.


Example:

Deny from all Allow from 192.168.xxx.xxx






Manage cookies


The .htaccess allows you perform operations on your visitors' cookies. You can create cookies, modify them, 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's easier for visitors to read.





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






Customise the default error pages:


Replace the default error pages (404, 401, 403, 500 etc.) with ones in harmony your website theme.


Example:

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






Manage redirects:


Guide your site visitors with temporary or permanent redirects.





Example:

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