Tags

, , , ,

Jurney-to-htaccessWhat is htaccess?
htaccess: Hypertext Access, is basically a configuration file which controls the directory in which it is placed and all the sub-directories underneath it. This file generally used on Apache based web servers to control many features of the server. It allows webmasters to control work behavior of their website. Following are the most common tasks generally every webmaster do using htaccess file:

  • Redirect pages
  • Change the extensions of pages
  • Password protect directories
  • ZIP the content before send from server to browser.
  • and much much more.


In next few minutes we will see most basic tricks and tips or you can say functionality of htaccess file which will definitely helpful for every webmaster.
Be careful!!
HTACCESS is a very basic small text file. A file with extension .htaccess is know as HTACCESS file. Most of the time this file resides in root folder/directory of any project. This file gives you a lot of control over what happens on your site but accidents can happen anytime so make sure you to keep a backup before updating the htaccess file. A small mistake can break your site so be careful with updating the htaccess file.

How to make your htaccess file private?
This is very important that no one can see or browse your .htaccess file. And to do so all you need to do is write the below code in your .htaccess file.

# Make your htaccess file private
 order deny,allow
 deny from all

Prohibit Directory Listing
If you does not have index file in a directory, then any one can see all the files in that directory from browser which arise security issue. For example, a most common directories which almost all webmasters forget to hide is the images directory which allows everyone to view all the images in their images directory.
To stop this all you need to add the following code in .htaccess file

IndexIgnore *

Here * is a wildcard and stops the server from listing any type of file. You can only stop certain files or file types from being listed.
Like :

IndexIgnore *.gif *.jpeg *.png myresume.pdf

The above code would stop all gif, jpeg and png image files from being listed. The myresume.pdf document would be blocked too however all other .pdf files would be shown. Mean the IndexIgnore command lets you decide what files in a directory visitors can see.
To block people viewing the files at http://www.yoursitename.com/images/ and http://www.yoursitename.com/banners/ you would the following code to your .htaccess :

IndexIgnore /images/*

Change the Default File of Directory
When you open a directory on the web, say http://www.yoursitedomain.com or http://www.yoursitedomain.com/sub_directory/, the apache server usually find the index.html file. If it can’t find then it starts looking for index.php or index.cgi. Index.html is generally the page with the highest priority and the one which is load prior to others.
Now the question is, is it possible to change the default page of a directory. Answer is YES. Say, for example, you wanted visitors to go to example.html instead of index.html. All you have to add to your .htaccess file is

DirectoryIndex example.html

The above code can be extended so that the server finds for other files if it cannot find the first one.

DirectoryIndex example1.html example2.html example3.html

Priority decrease from left to right. So the server would look for example1.html first. If that file is not there it will look for example2.html, then example3.html.

Redirect all URLs to root index.php file if url does not reffering to direct file or directory
This kind requirement you will get in all of the CSM and Frmaework. To do the same add the below code to HTACCESS file:

#If url not reffering to file
RewriteCond %{REQUEST_FILENAME} !-f
#If url not reffering to any directory
RewriteCond %{REQUEST_FILENAME} !-d
#If url not reffering to favicon
RewriteCond %{REQUEST_URI} !=/favicon.ico
#Then redirect to root index.php
RewriteRule ^ index.php [L]

How to setup a 301 Redirect
HTTP response status code 301 is a way of informing to search engines that a link of browsed website has been permanently moved to another link on the web. This kind of redirection is very needful if you have changed the structure of your websites url’s or if you have moved domain. Using htaccess you can redirect your entire website.
There are other similar type of response code present like 302 code informs search engines that link has been temporarily moved.
Again 303, which is means ’see other’ and the page has been replaced by something else.
So let see how to do 301 redirect using htaccess file
The basic format of code for redirecting is :

Redirect redirection_number old_location new_location

Here the old_location should be the absolute path from the root of your server. The new_location should contain http. Lets see an example, if you want to move a file called contactform.html from the root of your site to a sub-directory called contact all you need to write the below code to htaccess file:

Redirect 301 /contactform.html http://www.yoursite.com/contact/contactform.html

If you have moved your domain to another site, in this case you may want to redirect the whole site and to do so you have to simply use the following code :

Redirect 301 / http://www.yournewdomainname.com/

Redirect 404 error to a customized page
404 error message is the well known HTTP response code which is returned when a server unable to retrieve the requested link on the server. Like, if you change the url structure of your website and move a certain directory or file to a different part of your site then if anyone trying to access the old page url will get a 404 HTTP error message.

If a visitor visit your site and found 404 error message then it is quite possible that they will not try to see any part of your site. So, it is very important to redirect visitors from incorrect urls to this custom page where they can find other related links and they can feel that they are not lost on the web.
Fortunately, htaccess makes this very easy for us. To do the same first you need to create a 404 error page. For example, you would create a page at http://www.yoursite.com/404.php which says some proper message(available so many on net).
Once you are done with creating 404 page, all you need to do is send visitors from incorrect url to this page and this can be easily done by just adding the following line to your .htaccess file :

ErrorDocument 404 /404.php

Redirect users from Non-WWW version to WWW version of your site-domain
To prevent the duplicate content comes in search engines you can force your site visitors to use either the www or the non-www version of your website domain. This will avoid search engines such as Google/Yahoo/Bing indexing two versions of your domain.
It really doesn’t matter if you use http://www.yoursitename.com or yoursitename.com. I personally use www on most sites I own however many people prefer to drop it, it’s really up to you.
Force users to use http://www.yoursitename.com
To bind users to use the www version of your site domain all you have to do is add the following code to your .htaccess file and replace yoursitename.com with your site domain name.

# Redirect non-www urls to www
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule (.*) http://www.yoursitename.com/$1 [R=301,L]
Advertisements