WordPress Permalinks Causing 404 Error

This blog is a selfhosted WordPress instance. When I initially set it up, I had used the default settings of the permalinks section:


WordPress Permalinks Settings

After some time, I wanted the post urls to refer to the post title, so I selected “Post name” instead. That’s when all heck broke loose.

When clicking on a post from the main page, I kept getting the dreaded 404 error initially:

This is where I went down my rabbit hole. As usual, I googled for solutions, and the first thing I found told me to open up the apache2.conf and make sure the AllowOverride All was set. Interestingly, I did not even have an entry of nyctechtips.com, so I added:

<Directory /var/www/nyctechtips.com>
Options Indexes FollowSymlinks
AllowOverride All
Require all granted
</Directory>

I restarted apache after this, but no change. Found another post that said I had to make sure mod-rewrite was enabled. So I tried:

sudo a2enmod rewrite

but got command not found. Now I know this is an Ubuntu command, and I’m running Ubuntu 18.4, so it should be there. The post then gave another method to enable it, by manually moving the module files:

sudo mv /etc/apache2/mods-available/rewrite.load /etc/apache2/mods-enabled/

After another apache restart, no change with my permalinks.

I noticed the mods-enabled folder all had symlinks and not actual files. So I deleted the rewrite.load file from that folder and instead created the symbolic link:

sudo ln -s /etc/apach2/mods-available/rewrite.load /etc/apache2/mods-enabled/rewrite.load

Here, I inadvertently left out the “e” in apache. But it created the wrong link anyway (dug my rabbit hole deeper).

So when I tried to restart apache now, I got:

Job for apache2.service failed because the control process exited with error code.
See “systemctl status apache2.service” and “journalctl -xe” for details.

Now I tried the sudo a2enmode rewrite again, and this time it worked, giving the following message:

Removing dangling link /etc/apache2/mods-enabled/rewrite.load
Enabling module rewrite.
To activate the new configuration, you need to run:
systemctl restart apache2

 

Still nothing after another apache restart.

Next thing was the .htaccess file that was located in the same folder as my wp-config.php file. It should have:

# 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

Added the above, restarted apache, now I’m getting 403 Forbidden errors and can’t get to the main page!

This actually was progress. Checking the /var/log/apache2/error_log, I found the following error:

[Tue Apr 14 09:02:31.080955 2020] [rewrite:error] [pid 5361] [client x.x.x.x:64097] AH00670: Options FollowSymLinks and SymLinksIfOwnerMatch are both off, so the RewriteRule directive is also forbidden due to its similar ability to circumvent directory restrictions : /var/www/nyctechtips.com/wp-admin/options-permalink.php, referer: http://nyctechtips.com/wp-admin/options-permalink.php

Doing another google search I found that I had to add “Options +FollowSymLinks” to the start of my .htaccess file.

Options +FollowSymLinks
# 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

Restarted apache one last time and viola!! Working permalinks and webpage!! So ultimately, I think if you’re running your own hosted WordPress instance on Apache/Ubuntu and you’re trying to get the permalinks to work, make sure of the following:

 

  • Make sure you have the AllowOverride All entry in the apache2.conf file under the <Directory> section for your domain

Hopefully this helps someone else who falls down the same rabbit hole.

Leave a Reply

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