How to Create and Edit an .htaccess File for Your Website
The .htaccess file (shortened from Hypertext Access file) is a server configuration file that is typically used to control the behavior, security and performance of your website. Most web developers create and edit an .htaccess file and use it to control their websites and make sure everything is running smoothly.
How to Create and Edit an .htaccess File
To create and edit an .htacess file is actually extremely easy. Here are a few easy ways how you can do that:
How To Create an .htaccess File
There are two main ways you can create an .htaccess file for your website:
How To Create an .htaccess File – On your computer
- For Windows users: Create a new file using Notepad and save the file as .htaccess
- For Mac OS X users: Use TextEdit, create new file and save it as “.htaccess” (if you save the file as .htaccess it’ll be hidden and you won’t be able to see it in the Finder).
- After that use your preferred FTP client and upload the file to the server. Your .htaccess file needs to be placed in the folder where your site is located (usually the public_html folder).
How To Create an .htaccess File – On the server
- Login to your cPanel Account and click on the File Manager icon, located in the Files section. From the File Manger popup choose “Choose Hidden Files (dotfiles).” (if you don’t see this popup you’ll need to click the “reset all interface settings” link at the bottom of the cPanel page).
- From the File Manager popup choose the folder you’d like to open, usually the “Web Root (public_html/www)”.
- Click the Go button. (if you’re in the File Manager already you can add &showhidden=1 to the end of the URL).
- Click on the New File icon to create a new file and name it .htaccess
How To Edit an .htaccess File
Before editing your .htaccess file we suggest that you make a copy of your existing .htaccess file in case something goes wrong.
- Log into your cPanel or FTP program (like Filezilla).
- Locate the .htaccess file, right click on it and click Edit from the menu. If a dialogue box appears and ask you about encoding, just click Edit to continue.
- The editor will open in a new window. Edit your .htaccess file as needed.
- Click Save Changes in the upper right hand corner when you’re done.
- Make sure to test your website to see if your changes were successfully saved. If not, correct the error or go back to the previous version of your .htaccess file until your website works again.
- Once everything is complete, you can click Close to close the File Manager or FTP program.
Why you need to create and edit an .htaccess file
There are couple of reasons why you wold like to create an .htaccess file. Some of them are:
If you’ve migrated your website to a new domain, you would probably like to redirect your old domain URLs to the new one. This actually is probably the fastest and easiest way to do hat without loosing too much SEO ranking. You can also use the .htaccess file to redirect to a nice looking 404 page if some of your visitors visits a deleted page or mistypes an URL.
If you like your URLs to look nice (especially if you use CMS we know how long and messy they can be) you can definitely use .htaccess to shorten and clean them up. For example if your URL is example.com/index.php/contact/ you can easily turn it into example.com/contact/ with a few lines of code in your .htaccess file.
Authorization and authentication
You can use the .htaccess file to control who can view and which pages of your website can be visible to others. This is especially useful if you have plenty of spam clicks and comments. With the .htaccess file you can easily block domains and IP addresses that you think are unauthorized to visit your website.
The .htaccess file can help you to make your website load faster and perform better by reducing your website’s bandwidth usage. This is extremely helpful if you’re hosting your website on a third-party server and you need to pay for the bandwidth. The two main areas where you can increase your website’s performance are: Cache control – you can specify what file and how long it can be cached and prevent the server from querying the database when it’s not necessary, and Compression – you can compress your files so that they’re sent to the browser in a smaller piece.