The .htaccess file is a crucial component of your WordPress installation. It enables permalink functionality and can also be customized to enhance your site's performance, security, and redirection capabilities.
Essential Code for Your .htaccess File
For WordPress to function correctly, ensure your .htaccess file contains 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
Locating Your .htaccess File
To find your .htaccess file in your WordPress installation:
- Log in to your cPanel account using your credentials.
- Navigate to Files > File Manager.
- Double-click on the
public_htmldirectory.
By default, cPanel hides dotfiles, so if you don’t see the .htaccess file, follow these steps:
- Click on Settings in the top right corner.
- In the preferences dialogue, check the box for Show Hidden Files (dotfiles), then click Save. The page will refresh, and you should see your
.htaccessfile.
Creating a New .htaccess File in cPanel
If you've migrated your website and the .htaccess file is missing, you need to create one to ensure proper WordPress functionality:
- In your cPanel account, navigate to the
public_htmldirectory. - Click on + File in the top left corner.
- Name the file
.htaccessand click Create New File.
Once created, your .htaccess file will typically appear below the wp-includes folder.
Adding the WordPress Code
To add the necessary code to your new .htaccess file:
-
Highlight the
.htaccessfile by clicking on it. -
Select Edit from the top menu.
-
Click Edit again in the dialogue box that appears.
-
Copy and paste 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 -
Click Save Changes in the top right corner.
Addressing 404 Errors After Migration
If you encounter a ‘404 error’ after migrating to your new host, it may be due to a missing .htaccess file. This file controls your site's permalink structure, so while your homepage may load, other pages might return a 404 error. If this occurs, follow the steps above to create a new .htaccess file.