Introduction

I wanted to change my blog from the ugly default links of database entries to something more meaningful and pleasing to the eye. I decided to use “post name” as the permalink settings but low and behold it wasn’t so straightforward as I thought of just changing the permalink settings in the WordPress admin console.

Troubleshooting

I searched all over the web and of course found the solution as you can see the post name is indeed in the URL. The method I used to troubleshoot was to use one browser for modifying the WordPress install and another for testing while also having two SSH sessions open, one for file access using WinSCP and another for terminal access.

A2enmod

The first step was to find out if the rewrite module was loaded. This is an easy one liner:

sudo a2enmod rewrite

It wasn’t. Then an obvious restart of apache

service apache2 restart

Test using my other browser. Still 404 errors on all other pages other than home. Okay what next.

.htaccess

The next step was the .htaccess file as I understood it this file was being modified by WordPress to tell it to rewrite the urls. Yes the required information was actually present in the file

The file is located in the following directory:

/var/www/[Your Folder] or /var/www/html 

if like me you using the default location. So the information required in this file is the following:

# 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

This was all present so what next. Well this is where I got to the default conf file.

000-default.conf

The location of this file is as follows:

/etc/apache2/sites-available/

Now this file contains info on your website and by default is very minimal. By default it contains your listening port, DocumentRoot path and log details. I added the following info as advised by several posts:

DocumentRoot /var/www/html
<Directory />
Options FollowSymLinks
AllowOverride All
</Directory>
<Directory /var/www/html>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>

Still no cigar though, hmmm……however then i was led to this wonderful little snippet of gold:

https://www.digitalocean.com/community/questions/404-not-found-after-changing-permalinks-to-post-name-in-wordpress

Apache2.conf

Just a similar small line of code required but in a different file, this is the location:

/etc/apache2/apache2.conf

And this is the code that fixed it:

<Directory /path/to/site>
#add the following setting to allow .htaccess in your web dir to work
AllowOverride All

#other settings —

</Directory>

So now near the bottom of the file on my server it reads as follows:

<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>

This was the one that did it for me. Working!

Bear in mind that this took me some time with multiple reboots but hopefully this post will make it nice and quick next time a server needs the permalinks changing for me and for you!

 

 

 

 


 

404 Errors With Permalinks Set To %postname%
Tagged on:             

One thought on “404 Errors With Permalinks Set To %postname%

  • 29th December 2022 at 01:49
    Permalink

    You saved my butt!

    For people using Docker for their Apache+PHP install, this command works well for modifying the Apache config file:

    RUN sed -ri -e ‘//,// s/AllowOverride None/AllowOverride all/’ /etc/apache2/apache2.conf

    Reply

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.