How to Create and Edit .htaccess File in WordPress?

When WordPress was first introduced in 2003, there was little thought regarding its leap to what it is now. Not requiring any programming knowledge made it easy for individuals to manipulate it according to their needs. Today, WordPress even hosts e-retail and travel blogging sites. However, what appeals even more is its simple interface. Also, you can switch to the paid version of WordPress right when you want to mint some money.

The .htaccess File in WordPress

 What is .htaccess file?

The .htaccess file is a very useful core file in WordPress. It handles the configuration of servers – mainly the Apache server. This utility file of WordPress helps it to manage files in its root directory and sub-directories. There are other configuration files in WordPress too. However, as a WordPress blogger, you should keep in mind that they are out of your bounds for manipulation. So, it is only the .htaccess file that would let you tailor your server-client relationship. Tailoring fits well here. You can handle the reaction of your server to the influx of client requests. 

The file consists of simple code – not the one that hard-core programmers write. Also, you don’t need to learn this code language, the official website provides varying code for different purposes that can be added to this file.

Where to find the .htaccess file?

 The .htaccess file is a configuration file, therefore it is hidden. To trace it, first, you need to change your folder settings using the File Manager to allow the display of hidden files. Then, you need to go to the Root Document of your domain which is the public_html directory. There, you will be able to see your .htaccess text file.

 Utility of the.htaccess file

There must be something striking regarding this file that makes it so popular among bloggers from the non-coding arena. It is the dominating nature of this file that can change the server settings to offer an advantage to its master – the blog creator. It optimizes your site and also allows you to grab an audience by creating a user-friendly URL. It can store the cache for your website, and implore search engines to prioritize your site over those of others.

However, WordPress is smart enough to automatically reconfigure this file according to your chosen settings. But there are times when it won’t do it for you – it would rather ask you to do it manually.

 Creating the .htaccess file

The WordPress .htaccess file contains certain default content. This content is in the form of a code.

If this file is not available, you can follow the steps beneath to create it:

  1. Open the Notepad program in your system.
  2. Save the empty file with the name htaccess.txt.
  3. Now, inside the file copy the following content:

# BEGIN WordPress

RewriteEngine On

RewriteBase /

RewriteRule ^index\ .php$ – [L]

RewriteCond % {REQUEST_FILENAME} !-f

RewriteCond % {REQUEST_FILENAME} !-d

RewriteRule . /index.php [L]

# END WordPress

This is the default content of the .htaccess file.

  1. Save the file. Then, rename it to .hatccess and upload it to the root server of WordPress.
  2. However, if you have any issue in uploading the renamed file in step 4, you can first upload it to the server and then rename it there.
See also  How to Use WP-Optimize Plugin - Get Fast Loading Website and Rank Better in Google

With the above steps, your .htaccess file has been created.

Editing the .htaccess file

The most common thing that is manipulated using the .htaccess file is the permalink of your site. Permalink is the URL of your blogs. Split the word and you would be left with a permanent link – which is what it is. It links the content of your site with the server.

There are many ways to edit the .htaccess file. However, the safest and easiest method is to use the Htaccess editor. It is a plugin provided by WebFactory Ltd. on WordPress. It is popular due to features like testing syntax errors in the code and backing-up changes made to the .htaccess file automatically. 

The following steps will help you to edit your .htaccess file:

  1. Install and activate the Htaccess Editor plugin if it is not present on your site dashboard. 
  2. Go to your site’s dashboard. 
  3. Select Settings and then the Htaccess editor admin page. This will launch the .htaccess file with the default code and three options. These are – save changes, test before saving, and restore the last backup. 
  4. Now include the code you want and save the file. 

There are many changes that you can make to your file. Let us view the Apache server code for some of them.

  • If you want that your site displays a particular file when a client clicks on a link, include the following code :

DirectoryIndex index.php index.html /index.php

You can specify multiple URLs so that the server is capable of serving the client with an alternative if the default file is causing some error. 

  • If you want the files requested from your site to get downloaded to the client’s system, instead of merely being displayed, use the following code :

AddType application/octet-stream .avi .mpg

This command will force a Save As dialog box to appear on the client’s screen. 

  • If you have some confidential files, and you wish to share it only with some specific IP addresses, use the following code:

Order deny, allow

Deny from all

Allow from 120.63.0.108 localhost Do not worry – you do not need to learn these code snippets. You can find one according to your needs from the official WordPress site.