Apache is one of the most famous and stable server platforms, powering a very big part of the web servers around the world. And just like the PHP scripting language, Apache's behavior can be configured via a single file. And while with PHP this file is php.ini, the main Apache configuration file is httpd.conf. However, there are ways in which every user on an Apache-powered web server can modify Apache's behavior - with the help of a single .htaccess file.
The .htaccess file
The .htaccess file is a simple text file, which contains directions to the web server. You don't need to use the complete Apache configuration file in an .htaccess file and change the directives there, you can simply enter the ones that you wish to use in the .htaccess file and you are set. However, keep in mind that a single mistake can cause a problem with your website's online availability.
With the .htaccess file you can set access restrictions to different folders, redirect web page URLs, and much, much more.
Authentication with an .htaccess file
With the help of the .htaccess file you can easily protect your folders from the public eye, making them private and available only to a selected group of users. This way, you can safely hide all your sensitive information from unwanted visitors. To do that, place an .htaccess file in the folder you wish to protect with the following code:
.htaccess authentication
AuthType BasicAuthName "My Protected Area"
AuthUserFile www/my-best-domain.net/protected/.htpasswd
Require valid-user
You can change the “AuthName” with any name you want – this is the name that will appear in the Username and Password pop-up box. The “AuthUserFile” must point to the .htpasswd file in the protected folder.
The .htpasswd file holds the list of usernames and their respective passwords allowed to access the folder. The passwords are encrypted with MD5 hash to provide better security. You can use an MD5 hash generator to generate the password, or an .htpasswd file generator to generate the whole file.
An example of an .htpasswd file
test:$apr1$MwrwJ/..$pwkKIPcMshM7zeHhCdoPe.Redirection with .htaccess
Another very common usage of the .htaccess file is to enable URL redirection. This can be done for a single file or for your whole website in a very easy manner.
For example, to redirect an old web page to a new one, all you need is just one line:
Redirect /oldpage.html newpage.htmlThis way, all requests for oldpage.html will be redirected to newpage.html.
With the help of the .htaccess file you can also redirect your whole website:
RewriteEngine onrewritecond %{http_host} ^olddomain.com [nc]
rewriterule ^(.*)$ http://www.newdomain.com/$1 [r=301,nc]
The code above will redirect all requests for olddomain.com to newdomain.com.
Using .htaccess with NTC Hosting
As already mentioned, when dealing with an .htaccess file, you have to be very careful with the syntax you are using, since a single wrong line can affect your website’s online availability. This is why we have developed an easy to use .htaccess generator integrated in our Web Hosting Control Panel. This generator comes with both our cheap hosting offers and our CMS hosting plans. With its help you can easily generate the needed code in the needed folder without much hassle and without the need to find the exact code you need.