WordPress Permalink Stops Working

I recently migrated to a new Linux server with a different version of Apache, and for a while I could not get the WordPress permalinks to work. After a lot of trial and error, I figured out what the problem was, and I hope the information I share here will help someone in the future.

Below are the relevant system information:
OS: Ubuntu 14.04 LTS
Web Server: Apache 2.4.18

What was the initial symptom?

The home page of the blog loaded OK. However, when I clicked on any link to go into any post, category, or month, I got a 404 error.

For those of you who wants to know the answer right away without reading through my journey of getting there, go to the Fix section.

Things I tried

I tried a number of ways to resolve this issue. They were as follows:

1. Disabled all plugins. This did not help. The same 404′s still appeared after all the plugins were disabled. At this point, I concluded the plugins were not the issue.

2. Used a default theme. This did not help. In fact, I tried several different themes, and I kept getting the same error regardless of which theme I used. So, theme was not the issue, either.

3. Changed the permalinks to the default URL. This change fixed the issue! However, this is not a viable solution as I have no interest of changing my blog URL to something like https://www.1keydata.com/blog/?p=200. However, this did tell me something useful–the issue was related to rewriting the URL.

4. Changed the permalink to include index.php as part of the URL. For example, https://www.1keydata.com/blog/index.php/2016/03/february-2016-browser-market-share.html. This change also fixed the issue. However, this is not an ideal solution because I’d have to put 301 redirects on all my prior posts, category pages, tag pages, and month pages to eliminate the 404′s. This was not something I wanted to go through.

At this point, this appeared to be some kind of Apache URL rewrite issue. But I already have the appropriate .htaccess file in the /blog directory. So what could be going on? My theory was that for some reason Apache was not set up to read .htaccess files.

To fix this, my first try was to add “AllowOverride All” to apache2.conf under the appropriate directory. However, when I did this and restarted Apache, I saw a 500 error when I visit the website. Unfortunately, this method did not work.

Then, I remembered that whatever you can put in .htaccess, you can also put it in the server config file. This led me to the fix:

Fix

Take the .htaccess content that WordPress wants you to use, which should look like the following:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteRule ^index\.php$ – [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>

 
paste this content into your example.com.conf in /etc/apache2 using your favorite Linux text editor such as vi, emacs, or nano. After that, restart apache by typing in

sudo service apache2 restart

 
Viola! My desired permalinks now render okay! This is a fairly elegant solution because it adheres with what Apache recommends (not using .htaccess file). Also, it is a good idea not to have the “AllowOverride All” directive on because when it is on, Apache will first search for the presence of .htaccess at each directory level. For example, if your URL is

www.example.com/123/456/789.html

Apache will look for the following .htaccess files:

www.example.com/.htaccess
www.example.com/123/.htaccess
www.example.com/123/456/.htaccess

Not having to go through these files improves Apache’s performance.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>