A Super Simple Way to Create a New HTACCESS File for WordPress
We’re in the process of moving a ton of websites into a custom managed DigitalOcean cloud hosting environment and to say there have been some roadblocks would be an understatement. Some very complex and challenging, some very simple and seemingly stupid. This is to be expected, of course. No matter how handy you are with moving large databases things will inevitably break. Things completely out of your control.
One of the problems we had was corruption in .htaccess files after the move was complete and domains began resolving with the new DNS. This meant the websites were refusing to display internal pages outside of the main root domain. We would get a wide range of 500 and 400 errors depending on the page/file being requested.
We have plenty of server resources. We have all the proper PHP requirements met and databases are setup properly. But those pesky pages just wouldn’t work.
It was determined the likely cause, as mentioned above, was corruption in the htaccess file. For those who aren’t familiar with htacess this is a file that tells a browser what the rules are for a particular website and its handling of a domain.
WordPress.org explain it in a more geeky way.
The .htaccess is a distributed configuration file, and is how Apache handles configuration changes on a per-directory basis.
WordPress uses this file to manipulate how Apache serves files from its root directory, and subdirectories thereof. Most notably, WP modifies this file to be able to handle pretty permalinks.
If your htaccess file is corrupt or doesn’t exist your site will not handle internal links correctly. It won’t pull up files in a directory when a page or post is requested and will instead return an error code to the user requesting the URL.
The WordPress htaccess file generally contains the following calls.
You must have these rules and conditions running on your WordPress install for all internal pages/posts to work when requested. Without these in place all of your URLs are just going to return errors.
Now most forums and blog posts will tell you that to fix a corrupted .htaccess file you should go into your server through SFTP or FTP, find the file, make a copy locally then delete it. After this WordPress “should” re-create a new one. But for whatever reason this was not happening for us.
I suppose we could have just created a new one and uploaded it, but that was a bit tedious when it happened to a lot of our sites during this move. For us the simplest of solutions was to login to the WordPress dashboard, navigate to Settings>>Permalinks and hit save on the option we were already using (Day and name). Upon a successful save a new .htaccess file is created.
And just like that the entire site is fully active, responsive and functioning the way it’s supposed to.
Of course, this method requires that you’re able to access your dashboard. If you can’t access your dashboard you’re going to have to upload a new .htaccess file through FTP/SFTP. The file belongs in your public_html folder (same folder containing wp-admin, wp-content, wp-includes and all core wordpress files).
In any event just hitting save on the permalinks settings page saved us a lot of time and headache. It created a new .htaccess files with the default simple rules written in and we’re good to go.