ToolsToBlog_logo_quality_Final
Bloggers Library

Create htaccess file

What is a .htaccess file?

.htaccess is a localized configuration file that is used to control how an Apache web server responds to web requests from clients. The name .htaccess stands for hypertext access. It is used to control settings for the directory, subdirectories, and files within the directory where .htaccess resides.

Where to find .htaccess file?

Generally, it resides in the root directory like public_html. It may not be visible in Cpanel initially as it’s a hidden file. So you need to go to settings of the Cpanel and check show hidden files.

Can I have multiple .htaccess files?

Yes, you can have multiple .htaccess files each for a directory.  It will apply for the directory it is in and for all the files and subdirectories within this directory.

How to edit the .htaccess file?

Changes should be carefully done to the file as any error in syntax will render your site down. Please ensure to take a backup of the file.  It can be edited from

  1. Cpanel
  2. WordPress Dashboard through Yoast SEO plugin

1. Editing from cpanel

Open the cpanel and go to the public_html directory(Ensure hidden files are shown). Select the file and click on edit in cpanel. It will open the editor window.After editing click on save and close. Another way is to download the file by selecting it in cpanel and edit it in a text editor and save it and upload it to through the cpanel upload option.CPanel

CPanel

CPanelCpanelCpanelThe default .htaccess has the following code

# BEGIN WordPress
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress

2. Editing through WordPress dashboard using Yoast SEO plugin

Another option to edit the .htaccess file is to install the Yoast SEO plugin in WordPress.

Click on tools in Yoast SEO and click on File editor. It will open the .htaccess file by default.

Wordpress-Dashboard-htaccess

How to Create a .htaccess file if you don’t have one in the default WordPress installation.

If the default .htaccess is missing in the WordPress installation root directory, you can create one using the following method

Open Notepad and copy-paste the default .htaccess code listed above here in notepad. Now click on the File menu in notepad and click save as. Enter file name .htaccess in double-quotes as  “.htaccess”

Create_htaccess

It should be exactly like in the above image and there should not be an extension attached to it. Click on save and save it. Now upload the file to the WordPress “pubic_html” directory or whatever is your root directory using the upload option in Cpanel.

Do the directives used in .htaccess always executed?

It depends upon what the settings are there in your apache httpd.conf file or server configuration file and what directive you are using.If the access is restricted through options mentioned in httpd.conf, your directives issued in .htaccess won’t be able to show you any changes if they require permission to be set in the server configuration file which is httpd.conf.Basically, if you are on shared hosting you might not have access to httpd.conf but still, the settings are as such, it allows you to issue instructions to the Apache webserver through .htaccess directives.

So the httpd.conf needs to have the following directive for all the .htaccess directives  to work

AllowOverride All

This has a performance downside to it that it will search for .htaccess in all the directories every time a file is requested whether it is there or not.

If you want to disable .htaccess, set the Allowoverride to none just like below in main server configuration file

AllowOverride None

Does use lots of directives in .htaccess take a performance hit?

Yes, each time a file is requested, directives in .htaccess are applied. If the same directive is issued in the server configuration file it occurs only one time when the server config file is loaded. So the use of directives in .htaccess should be minimal and only the required ones. If you have access to the server configuration file consider placing these directives in the server configuration file.

In what order the directives be applied if multiple .htaccess files are used?

Let say first you have example.com/dir1 and there is .htaccess file in it, then the directives will be applied to all the directories and files in dir1 and below it. Now let us say you have example.com/dir1/dir2 and there is a second .htaccess file in dir2. So for dir2 directories and files and below level directories and files for dir2, directives in .htaccess in dir2 will apply.

How can you use a different filename for .htaccess?

You can use the following in the server configuration file to rename .htaccess to your desired name.

Here I have renamed it to Directives.

AccessFileName “.Directives”

Please also refer to Uses of .htaccess