The .htaccess file plays a vital role in the management and security of a website. In this article, we will show you how to use the file to customize your site.
In particular, you will learn how to do the following:
- Create and edit the .htaccess file
- Customizing error pages such as 401, 401, 403, 404, 500, 501, and more
- Block visitors based on IP address, referrer or any other parameter
- Redirect pages and websites.
- Password protect directories.
Before we start customizing the website, let us see how to locate or create the .htaccess file, as well as how to edit it. Most often, the system file will be hidden, and you need to make it visible in the Settings.
- Login to your website’s cPanel and open the File Manager under Files.
- Open the public_html or your website’s root directory.
- Click Settings on the top-left corner.
The dialog box below will open.
- Tick the checkbox Show Hidden Files (Dotfiles) and Save.
If the .htaccess file is not there, you can use one of the various methods to create the file and save it in the public folder.
Creating the .htaccess File
You can use a text editor to create and edit the .htaccess file. Note that the field does not have an extension. While you can use notepad and then upload using an FTP client or legacy file manager, the easiest way is to use the inbuilt code editor in cPanel.
Alternatively, you can create the file using the command below after you’ve SSH’d to your Linux server. Replace the yoursite.com with your website. Note: The path to the .htaccess file shown below may vary depending on which web server software you’re using.
sudo nano /var/www/yoursite.com/.htaccess
Creating a new .htaccess file in cPanel
- Login to your website’s cPanel and open the File Manager.
- Open the public_html or website directory.
- Click on the + File at the left-hand top corner. A dialog box prompting you to enter a filename appears.
- Type .htaccess and click Create New File.
Create .htaccess using a text editor
Use a text editor such as Notepad, TextEdit or any other suitable tool. Create a new file and save it as .htaccess without an extension.
Use FTP or file manager to upload the file into the public_html folder.
Editing .htaccess file
To edit the .htaccess file, use the file manager to locate the file and right click on it.
- Login to your website’s cPanel and open the File Manager (figure 1).
- Open the public_html or website directory.
Check out these top 3 cPanel hosting services:
- Want to get top recommendations about best hosting? Just click this link!