A how-to guide to fix and restore the WordPress .htaccess file.
Restoring the default WordPress .htaccess file
The .htaccess file is used by WordPress installations that run on the Apache web server. This applies to almost all WordPress installations. However, if you are running the Nginx web server instead of Apache, please see configuring Nginx .htaccess replacement.
The default WordPress .htaccess is as follows:
# BEGIN WordPress <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] </IfModule> # END WordPress
The above .htaccess example is the default WordPress .htaccess. If you have installed a caching plugin there will be additional lines in your WordPress .htaccess. A redirection plugin will also add options to the .htaccess file.
Before making any changes to your WordPress .htaccess file, make a copy of the current file for reference and to repair your .htaccess if something goes wrong.
Warning:
If you changed your WordPress permalink structure, domain name, or prefix (ex. yourdomain.com to www.yourdomain etc..) in the past, you may need additional code in your .htaccess file. Without the additional custom code, visitors may not be able to reach your site from links on other websites. Visitors will receive a 404 not found error code.
A misconfigured .htaccess and 404 errors are also likely to cause your ranking in Google search results to suffer and organic traffic to drop.
Proceed with caution and consult technical support if you are not knowledgeable on this topic. Test all redirections after editing or resetting your .htaccess to the WordPress default.
If your .htaccess file becomes corrupted or missing simply edit or create the file in the root of your WordPress installation. Next, paste the above contents into your .htaccess file. You will need FTP/SFTP access to your server to create or fix the .htaccess file. Learn how to use Filezilla to securely connect to WordPress via FTP.
Since caching and redirection plugins also use the .htaccess file you may need to copy their code from your prior .htaccess file and/or disable and re-enable those plugins to make them generate their .htacess entries.
Compare your new .htaccess file with your backup file.
How to use .htaccess to redirect all traffic to your domain with www
Place the following lines in your .htaccess file before the #BEGIN WordPress entry to redirect requests from yourdomain.com to www.yourdomain.com.
RewriteCond %{HTTP_HOST} =example.com RewriteRule (.*) http://www.example.com/$1 [R=302,L]
How to use .htaccess to redirect all traffic to your domain without www
Place the following lines in your .htaccess file before the #BEGIN WordPress entry to redirect requests from www.yourdomain.com to yourdomain.com.
RewriteCond %{HTTP_HOST} =www.example.com RewriteRule (.*) http://example.com/$1 [R=302,L]
WARNING on Redirections
Always test your .htaccess redirect changes.
For example, after saving a modified .htaccess, type or paste a test URL into your web browser that tests the changes you made. If you have a post at https://yourdomain.com/the-best-post-ever/ and changed your .htaccess to add www. to your domain then paste that URL into your web browser. It should be automatically redirected to https://www.yourdomain.com/the-best-post-ever/ and your post should appear. If the change you made is not working properly, undo it.
Your site should not be accessible at both www.yourdomain.com and yourdomain.com, just one or the other.
Redirecting requests with .htaccess and adding or removing www. can have a significant effect on your website traffic.
If your redirections do not work properly, visitors who click on links on other websites that lead to your site will receive an error page instead of your posts.
Misconfigured redirects will likely cause a loss of traffic and a loss of search result ranking in Google.
Proceed with extreme caution and consult a technical or SEO expert if you are uncertain.