How do you Edit the .htaccess File?

Before you upload an .htaccess file to your server, make sure there is not already one there. Your host panel or perhaps a script you have uploaded may have already changed the htaccess for some reason so you don’t want to overwrite it as doing so could change something important on your site.


Htacess Changes in Cpanel

For example, in cpanel, you can setup 301 redirects very easily but this tool is simply a script which changes the .htaccess for you.

So even though you may not realise it, when you use the redirect script via cpanel, it updates your .htaccess file for you and it’s the .htaccess file which controls the redirect for you (the redirect tool just makes it easier for those who are not familar with .htaccess).

Htacess Created from Cpanel

So if there is an .htaccess file on the server already, you want to ensure that you download the htaccess file and then edit it before reuploading. This will ensure that nothing you or someone else has setup previously is changed.

If there is no .htaccess file there then you need to create one. Thankfully, this is very easy to do. All you need to do is open a text editor and save a blank document as .htaccess. Save the file exactly as it is stated there in bold ie. there is no writing before the extension. You need to save it as .htaccess and not htaccess.txt or document1.htaccess or whatever.


Htacess Upload to Server

When uploading you should always :

  • Upload in ASCII mode, not binary
  • CHMOD the file to 644 (this isn’t absolutely necessary per say but it’s advisable, it means your server can access it but it can’t be seen via a browser).

Htacess Comment Organization

Also, after editing your .htaccess file several times it may look a little complicated so I recommend adding a comment above the longer parts or sections of code so that you know what each section is for when you look at the file again at a later date.

Htacess Comment #

To add a comment to the htacess file you simply start the line with #, any code written after the # in the start of the line will not be executed.

Htacess Comment Line Examples

# Index Page
# Redirects 301 for Recent Pages
# Errors 404 Pages

This is simply a reference for you in the future (and anyone else who may be working on your website ie. co-admin or whatever).

Edit the .htaccess File Reference

Comment Organization: When you need to create, say more that 15-20 redirect, 404 errors, custom htaccess coding it can get confusing and a waste of your time searching for the line, especially if you or others have not been into the htaccess file for a while. Extra time grouping related htaccess code lines can save some aggravation search time.