htaccess can be used to control the access of the directories. I have just finished the academic project of “PHP based Job Portal CMS” and for this project I had to secure the config files. Though it is not live and it’s on the localhost but I just wanted to test htaccess so that when I install this project on a live server I would not give hackers access to my admin folder which currently sits on my C:\wamp\www\jobs\admin\config.php
I want to share with you the way of testing htaccess file on localhost and I hope you will understand this Easy Tutorial.
Let’s first know the basics of HTACCESS.
What is .htaccess files
Firstly a .htaccess file is simply an instruction file which instructs server how an WEB-ADMIN wants to give access to a directory of file. In Cpanel we can easily do this from interface . What cpanel actually does is it simply edits the htaccess file.
Htaccess file can be used for the following functions rewriting URLs, prohibiting hotlinks, reconfiguring account settings, creating redirects, and much more. Changing this htaccess file will simply change the configuration of your whole site. So you must take a backup first for an online site.
For our case, we simply want to create a .htaccess file on our localhost. So let’s jump into the process:
How to Enable .htaccess on WAMP / XAMPP / MAMP for testing in localhost
First Run your WAMP or XAMPP or MAMP. I will be showing you using WAMP on a Windows PC. You can configure XAMPP/MAMP with the similar settings. Now click on the WAMP icon which you will see on the System Tray . You will see something like this:
Now hover over the Apache settings and click on the httpd.conf.
This is the configuration file which we need to edit. So you have opened the file in notepad,right? alright. Now please scroll down until you find this line
#LoadModule rewrite_module modules/mod_rewrite.so
Remove the Hash sign from the beginning of this line so it looks like this:
LoadModule rewrite_module modules/mod_rewrite.so
Now Save the file.
Congratulations! You have just enabled htaccess on WAMP and you are now ready to unleash the power of htaccess even from localhost.
How to create a .htaccess file on Windows PC?
Now the most important part. First you have to decide which folder of your site you want to manage using htaccess file? Because you have to create .htaccess file on that folder. Suppose you have a folder in C:\wamp\www\jobs\admin and you want to revoke access of this folder for everyone. So you have to create the .htaccess file on admin folder .
First open the folder for which you want to control the acceess and create a text file and name it as ABC.htaccess .
Now open this file ABC.htaccess using Notepad and write the .htaccess code here.For this case I want to revoke direct access to admin folder so I put something like this “deny from all” in the ABC.htaccess file.
Remember there is ABC before .htaccess . Now you may ask me why? As you might already know that we can not simple create .htaccess file in windows computer because it thinks this a file without any name and it’s format is htaccess. Now the fun part.
How to rename ABC.htaccess to .htaccess
This is super easy. We can use Command Prompt to do that.
- First click WINDOWS KEY+R and Run window appears
- Now write CMD
- in the command prompt write this:
ren “C:\wamp\www\jobs\admin\ABC.htaccess” .htaccess
In this command ren means rename . This command will rename ABC.htaccess to .htaccess.
VOILA! You have successfully created a .htaccess file on your computer.