.htaccess techniques | Part I
.htaccess Techniques Developer should know
.htaccess file is one through which you can control the Apache server without modifying its htdocs file. This file allows you to do so mamy things with just little efforts.
1. Remove WWW from your URL
For SEO purpose you might need to remove the www prefix from your url, or sometimes you might prefer to use it.
Here is the way to remove the www from url by redirecting it to non www url.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^ehussain.com$ [NC]
RewriteRule ^(.*)$ http://ehussain.com/$1 [L,R=301]
2. Use Custom Error Pages
For your website you might prefer to use custom error pages. You may also want to load different pages on different kind of errors.
Here is the way to use error pages from .htaccess. Upload this pages to your server and change the location of them based on your need.
ErrorDocument 400 /errors/bad_request.html
ErrorDocument 401 /errors/authentication_required.html
ErrorDocument 403 /errors/forbidden.html
ErrorDocument 404 /errors/not_found.html
ErrorDocument 500 /errors/internal_server_error.html
3. Logging PHP Errors
Debugging is important part of development cycle. but when you are in production environment you might not want your user too see the internal server errors.
But instead you may want to log those errors in some file on your server. Also if you are on shared hosting you might not have access to configuration file to enable or change the location of error log.
Here is the way to log the php errors in file through .htaccess file.
# display no errs to user
php_flag display_startup_errors off
php_flag display_errors off
php_flag html_errors off
# log to file
php_flag log_errors on
php_value error_log /location/to/php_error.log
4. Prevent Hot Linking
Hot linking is a technique used by many website to load images from other website. So, if someone is hot linking to you then your bandwidth gets consumed for others benefit.
Here is the way to prevent hot linking to your files on your server. This will allow files to get loaded only if its being referred by your website.
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^http://(.+\.)?ehussain\.com/ [NC]
RewriteCond %{HTTP_REFERER} !^$
RewriteRule .*\.(jpe?g|gif|bmp|png)$ /images/do-not-hotlink.jpg [L]
5. Force Download
In your website you may offer download options at various place, and you might want browser to ask user to download file, instead of rendering it in browser.
For example, you might want browser to force the download for the files with extension of “mp3″ and “xls”.
Here is the .htaccess file content for forcing download.
<Files *.xls>
ForceType application/octet-stream
Header set Content-Disposition attachment
</Files>
<Files *.mp3>
ForceType application/octet-stream
Header set Content-Disposition attachment
</Files>
There are more techniques that I will come up with in Part II
-
Hussain Cutpiecewala | ehussain
Jun21












- Abdulqadir
- nirav gohel
- Rohit Patel
- eHussain
- Keisha Whiteis