As a self-hosted WordPress user you need to have a basic knowledge of editing files available on your installation. Some of the most frequently edited files include wp-config, functions.php and .htaccess. While most of the files are used to control the behavior of your WordPress site, .htaccess controls the Apache server level configuration. It can be placed on each subdirectory level to control the behavior of that specific directory. In this article we will explore the reasons why you need to edit .htaccess file and the ways to edit .htaccess file in WordPress.
Why You Need to Edit .htaccess File in WordPress?
There are many situations you need to configure the server to behave the way you want. Since you will not have direct access to configure the server, .htaccess is the way for a normal user to add the parameters. Some of the most required scenarios are as below:
- Adding server level redirect. For example, when you host your images on subdomain there may be a server level redirect required to point the media uploads folder to your subdomain.
- Other redirections like 301 redirects for pages, redirecting subdomains to main domain, redirecting to a different domain due to change of site name.
- IP address blocking
- Serving language dependent site based on geo-location.
A simple real time example is – When you set the permalinks for your WordPress site, this will also add rewrite rule in .htaccess file.
How to Edit .htaccess File in WordPress?
There are three ways to modify .htaccess file.
- Using plugin
- Through your cPanel hosting account
- Using FTP
There are many plugins offer the function to modify .htaccess file directly from WordPress admin dashboard. We will take an example of the popular Yoast WordPress SEO plugin. Once installed and activated the plugin, navigate to “SEO > Tools > File Editor” menu. You can edit robots.txt and .htaccess file directly from there.
Using cPanel Hosting Account
Login to your hosting account and navigate to “File Manager”. You can locate .htaccess file under the root directory of WordPress installation. Generally it will be available under “/public_html” directory. You can open and edit online with the text editor.
The last and most preferred option is to use FTP clients like FileZilla to remotely access your server. Locate the .htaccess file under the root WordPress installation. Right click and choose “View / Edit” to open the file with your local text editor. Check out the complete FileZilla guide for beginners.
Points to Remember
- When using FileZilla, ensure to enable “Force showing hidden files” option under “View” menu to see .htacces file. Generally it will be hidden on Mac and PCs, so you may not be seeing the file under local system section also.
- Always backup the current file before start modifying. Any small mistakes like a dot or comma will make your site inaccessible. So ensure to edit with care.
- Always use comments to indicate the purpose of the code so that it is easy to understand later point of time. Add ampersand symbol (#) at the start to make a comment line.