For those who are unaware of what WordPress is, let’s give you a quick introduction.
A large number of people looking to set up their own websites often find the process very resource-intensive and expensive. While some businesses may be able to afford top-quality bespoke web app development services, others may not be able to afford to hire specialized developers to make a brand new website for them.
So what do they do then? As a more affordable and easier solution, they turn towards content management systems (CMSs), and WordPress is one of the most famous and most-used CMSs in the world.
To put it simply, a CMS is just a template or a shell of a website. A CMS like WordPress would provide you with the opportunity to start your own website even with little to no prior coding experience or other technical knowledge. They will give you a website template. You can modify it to re-design your website’s appearance as you wish.
The world of WordPress is vast, with many topics to discuss. Today, however, let’s talk about one particular aspect of a typical WordPress project: the .htaccess file.
If you have any prior experience working on WordPress with Apache web server, you might have run into the .htaccess file for your project and wondered, “What does it do? Where is it located? Can it be modified? If yes, how?” It is also possible that you may have never heard of it before. In any case, let’s solve this mystery for you today.
What Is The .htaccess File?
According to WordPress’s own website, .htacess is a configuration file that helps in making your WordPress site work with the Apache webserver. The web server needs to be told what to do with your site links when they are requested by a browser. The .htaccess file facilitates this process and acts as a kind of instruction manual for your server.
Where Is The .htaccess File Located?
When you are using WordPress, you need to sign up for a cPanel account to manage your project files, among other tasks. Using your cPanel login, you can directly access your files that are hosted on the server. Your .htaccess file is also stored here, among these files.
Here’s how you can locate it:
Once you have logged into cPanel, click on Files, and then navigate to File Manager. Next, click on the folder named public_html. Make sure you have checked the ‘Show Hidden Files’ box in your Settings menu. Once you follow these steps, you will be able to see your .htaccess file in your public_html folder. If you want to edit this file, just right-click on it and select the ‘Edit’ option.
Sometimes, you might not be able to locate the .htaccess file in your public_html folder even after ticking the ‘Show Hidden Files’ checkbox. That means the file does not exist. In this case, if you feel that it is absolutely necessary, you are allowed to create a new .htaccess file yourself. All you have to do is go to the public_html folder and click on ‘+File’. It will open a new dialogue box giving you the option to enter the name and location where you wish to save your file. Enter ‘.htaccess’ for the former and ‘public_html’ for the latter. Then click ‘Create File’.
What Purposes Can The .htaccess File Fulfil?
The main purpose of the .htaccess file is to handle your website’s permalinks (permanent URLs for each page on your website) and maintain their proper structure. The .htaccess file is used to store cleaned up or “prettified” URLs so that random characters are eliminated and readable URLs are used to link to your pages.
However, that is not all. There is a lot more you can do with this file.
Suppose you move your old page, www.goodcore.co.uk/discussions, to a new location, www.goodcore.com/blog. Now, if a user goes to your old page, they will be unable to find it. For this purpose, you need to redirect them to the new location. You can achieve this easily with the .htaccess file. You just need to add this line to your file:
Redirect 301 /discussions.html http://www.goodcore.com/blog.html
You can also use this feature to enforce SSL, that is, redirect HTTP requests to the more safe and secure HTTPS, given that your site has an SSL certificate.
While there are plenty of ways to make your WordPress site secure, the .htaccess file is also very commonly used for security, such as for allowing or restricting particular IP addresses or domains from accessing your site. For example, if you want to allow only one specific IP address, i.e. 18.104.22.168, then you can add the following lines to your .htaccess file to grant this IP address access to your domain:
Order deny, allow Deny from all Allow from 22.214.171.124 localhost
You can also set the default language for all your files by adding just this one line to your file:
Similarly, if you want to serve a different custom error page for every error that is encountered when a client request comes in, you can add the following line:
ErrorDocument <error_number> /path_of_error_file
Now, if you want to serve the page ‘error/500.html’ whenever a 500 error is encountered, you will write:
ErrorDocument 500 /error/500.html
We hope that our discussion thoroughly clarified the .htaccess file and its various purposes to those who are new to WordPress and also to those who are already familiar with it. You should now be able to use this file to manage your site in a much better manner.