What is .htaccess

.htaccess is a configuration file for use on web servers running the Apache Web Server software. The .htaccess files can be used to alter the configuration of the Apache Web Server software to enable/disable additional functionality and features that the Apache Web Server software has to offer.

Creating a .htaccess file

.htaccess is the filename in full, it is not a file extension. For instance, you would not create a file called, name.htaccess or .htaccess.txt, it is simply called, .htaccess

You can create a new .htaccess file using any good text editor such as TextPad, UltraEdit, NotePad and similar.

Creating a .htaccess file may cause you a few problems. Writing the file is easy, you just need enter the appropriate code into a text editor (like notepad). You may run into problems with saving the file. Because .htaccess is a strange file name (the file actually has no name but a 8 letter file extension) it may not be accepted on certain systems.

With most operating systems, though, all you need to do is to save the file by entering the name as ".htaccess" (including the quotes). If this doesn't work, you will need to name it something else (e.g. htaccess.txt) and then upload it to the server. Once you have uploaded the file you can then rename it using an FTP program (e.g. FileZilla).

How to use .htaccess in Joomla!

Joomla! already have a file called htaccess.txt located in the root directory. Once you want to use this file you must rename it to .htaccess.

By default this file has been pre configured to work well with Joomla!, such as SEF, block out some common exploits,...

.htaccess Tutorial 

In this part I will show you how to use the .htaccess file to implement some of these.

Custom directory index files

The index.php file is the skeleton of the website. Every page that Joomla! delivers is index.php fleshed out with a selection of content inserted from the database. By default, index.html file will be executed first priority, so if you have this file in root directory you will get a blank page. To fix this, add to .htaccess the following lines:

DirectoryIndex index.php index.html

Hide and deny files

Sometimes you want to hide or prevent access to any file in your Joomla! folder (e.g. xml files)

<Files ~ "\.xml$">
Order allow,deny
Deny from all
Satisfy all
</Files>

Allow access to a specified file

But you want to allow access to some files (e.g. allow search engines access to your sitemap.xml)

<Files sitemap.xml>
allow From All
</Files>

Redirect www to non-www

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

Redirect non-www to www

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

Redirect Old domain to New domain

RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]

Or

Redirect 301 / http://newdomain.com/

Redirect to a Different URL

Redirect /category/old-article.html http://www.joomtut.com/category/new-article.html

Redirect Old category to New category

Redirect /old-category/article.html http://www.joomtut.com/new-category/article.html

Note that, this file will take effect when placed in any directory which is then in turn loaded via the Apache Web Server software. The file will take effect over the entire directory it is placed in and all files and subdirectories within the specified directory.