Htaccess Tutorial, Tricks and Techniques

1 Comments Posted In:

There are lots of different elements that you can accomplish with htaccess index file. They can stop visitors from hot linking your images and taking your bandwidth. Or create redirects, password protect directories and allow / block certain ips address from accessing your website.

htaccess File on Server

What is a htaccess file?

A htaccess index file sits usually in the root directory of your website and is most common with Apache severs. Named as .htaccess, this is the default name and file type the server will recognize. Mostly used for password protecting and directory management, it has also become a way to override some of the Apache settings.

How to create the file?

Creating a .htaccess file can be tricking on a client Operating System. The easiest way to get the file to work with the weird extension is to upload it to your server and rename it from there. Windows operating system will give you an error if trying to rename one. To create one with Windows use notepad and save as “All Files” and Encoding “ANSI.” This will give you an .htaccess file without errors and then you can upload the index to your server. After you upload your file to the server, check you CHMOD permission and see if they are 644 (RW-R--R--). Also some web hosting server will not allow you to use a htaccess file. Double check with your service before you stress out because it’s not working.

Save File Tutorial

Commands with htaccess File

Here are a few of the most common commands that are used with a htaccess file.

Custom Error Pages
Send user to specified index page on a website error.

ErrorDocument 400 / badrequest.html
ErrorDocument 401 / authreqd.html
ErrorDocument 403 / forbid.html
ErrorDocument 404 / notfound.html
ErrorDocument 500 /notfound.html

Password Protect Directory
Password protect a directory, place user name and password in a .htpasswd file in this format. - admin:password

AuthUserFile /var/www/.htpasswd
AuthGroupFile /var/www/html/users
AuthName EnterPassword
AuthType Basic

Require User admin

Block Users or Sites
These commands will prevent certain ips and websites from accessing your website.

Order Allow, Deny
Deny from 123.45.6.7
Deny from 891.01.1.2
Allow from all

RewriteEngine on
# Options +FollowSymlinks
RewriteCond %{HTTP_REFERER} blocksite\.com [NC]
RewriteRule .* - [F]

Change Default Index File
This command will give you the ability to change what your index file goes to when accessed.

DirectoryIndex newhomepage.html

htaccess index Redirects
Redirect www., new or old pages

RewriteCond %{HTTP_HOST} ^without.com$
RewriteRule ^/?$ http://www.with.com [R=301,L]

RewriteCond %{HTTP_HOST} ^site.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.site.com$
RewriteRule ^oldpage.html$ newpage.html [R=301,L]

Prevent Hot Linking
Hot linking is when someone is pulling your images index from your server and using your bandwidth.

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?domain.com/.*$ [NC]
RewriteRule \.(gif|jpg|js|css)$ - [F]

Conclusion of Tutorial, Tips and Techniques

There are even more elements and effects you can accomplish with htaccess. These are just some of the most common features web developers are using. It’s a good idea to know some of these tricks to help your website out. And to know what is possible for future problems you might have. So dig deeper and keep on learning.

Advertisement

Average: 5 (2 votes)