As a self-hosted WordPress user, you need to have the 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, let us 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 and force it to behave the way you want. Since you will not have direct access to configure the server, .htaccess is the way for normal users 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 includes the redirect rules added in your .htaccess file when you set the permalinks for your WordPress site.
How to Edit .htaccess File in WordPress?
There are three ways to modify .htaccess file in WordPress.
- Using a plugin
- Through File Manager app from your hosting account
- Using FTP
1. Edit .htaccess Using Plugin
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 “Yoast SEO > Tools > File Editor” menu. You can edit robots.txt and .htaccess file directly from there.
Note: If you do not see “File Editor” option, your security plugin setting might have removed it. For example, SG Security plugin which comes preinstalled with all WordPress installations in SiteGround will disable file editing options from admin dashboard (SG Security > Site Security > Disable Themes & Plugins Editor). In addition, you can also manually add a rule in wp-config.php file to disable file editing from dashboard.
2. Using File Manager from Hosting Account
Login to your hosting account and open File Manager app. 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 the file online with the text editor.
After modifying the content, make sure to save and re-upload on the server.
3. Using FTP
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. If you are new to FTP, check out the complete FileZilla guide for beginners.
Points to Remember
- Since .htaccess is the file with only extension (and no name), it will be hidden by default. When using FileZilla, ensure to enable “Force showing hidden files” option under “View” menu to see the file. Generally it will be hidden on Mac and PCs also, so you may not be seeing the file under local system section in FileZilla.
- 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.