What is .htaccess and what can you do with it (Part 1)

htaccess ti einai

The .htaccess file is a configuration file used on Apache-type servers.  With .htaccess we can handle a range of tasks on the server, for example protecting folders such as wp-content and wp-admin on a WordPress site.

Creating an .htaccess file is very easy. You simply create a file on your server, in the directory you want it to “apply to”,  and name it .htaccess.

Here is an example of an .htaccess file:

RewriteEngine On
        RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
        RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

The example above redirects example.com to www.example.com. We will look at this in more detail later.

Error Documents

It is good practice for your website to have an error document, meaning a page that tells visitors that what they asked for does not exist, so that they can look for something else. You can send visitors to such a page through .htaccess with the following directive.

ErrorDocument 404 /error_pages/404.html

This way, whenever a visitor looks for something that does not exist on your website, they will be taken straight to www.yourdomain.gr/error_pages/404.html

The 404.html file is a simple HTML file that can contain whatever you like.

Redirects

Redirects let us send our visitors from one location to another.

For one reason or another, we may want to change a URL to a different one. For example, we may want to make our website’s URLs more readable so that we rank higher in the organic results. SEO-friendly URLs are known to help a website reach better positions. The problem is that if we simply change a page’s URL, we create the 404 error we mentioned earlier.

Anyone who has bookmarked that URL, or who links to your website from theirs, will now land on a 404 error. The problem is even bigger with search engines such as Google, since a large number of 404 errors can even lead to lost rankings.

The solution is simple! All we need to do is add a rule to .htaccess that redirects the old URL of the page to the new one. To do this, we add the following rule:

Redirect 301 /old_page.html http://www.yourdomain.com/new_page.html

This tells both people and search engines that the page has moved permanently to a new location. If we only want to move it temporarily, we write the rule above without the 301, or we put 302 after the word Redirect. This tells search engines that the page has moved to a new location for now and will be available again at some point in the future. So take care to choose the right type of redirect: Redirect 301 is for a permanent move, while Redirect or Redirect 302 is for a temporary one.

 

Leave a comment

Your email address will not be published. Required fields are marked *

Recent articles

Ελλάδα 2.0 Banner

Free e-book from Web-Mate on local marketing