WordPress permalinks not working

WordPress permalinks not working? I feel your pain. Read on…

Recently, after setting up another virtual host on my Linux server, for a WordPress site, permalinks were not working. Any page other than home was getting a 404 error. I searched Google for help, but kept finding the same advise, which was basically to make sure that the .htaccess file was writable and had the correct permissions.

Sadly, it took me 2 days to solve this puzzle. (About 3-4 hours total.)

Come on, I’ve done this before!

This was the second time I was doing this, so I knew I had figured it out once before, and done all the work to learn how it is done, but for some reason the dots just weren’t connecting for me. It was very frustrating!

So now I am documenting it so that if I ever add another site, I’ll have a guide to remind me of exactly what is required to get it done in minutes, rather than hours.

The basic steps:

  1. Create site folder, and place the files for the website there
  2. Set permissions
  3. Create virtual host conf file
  4. Confirm that the site comes up insecure (HTTP)
  5. Use Let’s Encrypt to add SSL
  6. Confirm that the site only works in HTTPS mode

This is where things went wrong…

It was at this point that I realized that the permalinks were not working.

I began confirming that the .htaccess was correct. Then I remembered that I had done something to avoid .htaccess last time because I had read at least one page showing how it negatively affects site performance.

I started poking around the .conf file, adding in the Directory block, and making sure it contained the code that the .htaccess file had.

But the changes were not having any effect. I was a bit baffled as to why.

The Aha Moment

Finally, I realized that I was not in the right file! Let’s Encrypt creates a second .conf file, and that is where the code needs to be!

The first .conf file has :80 in it, and  handles HTTP traffic, and it redirects to HTTPS, so then the .conf from Let’s Encrypt is active. It has :443 in it, and that is where the code needs to be.

I opened up the :443 .conf file from the first site and immediately saw the code I was looking for. A few copy/paste’s later and the new site was working properly, and the mystery was solved.

The :443 .conf file has a Directory block in it, which tells the web server not to look for  .htaccess files. This improves performance. All that was required was to copy the .htaccess file content into that section.

 

Are you also a PHP programmer? I’ve got other blog entries that you might be interested in!

 

More about me

I have been programming for almost 20 years. It all started with Basic and RPG III way back in high school, and have played around with many languages. PHP is my main language today.

Some sites I have built:

For fans of BMX legend Scotty Cranmer: https://ScottyFan.com

Meet my Basenji’s Graham and Ginger: https://BarooBoos.com

Meet my newest Basenji, Zulu: https://ZuluJoy.com

chmod

chmod is short for change mode. It is the linux command used to change file permissions.

The basic format of the command is like this:

chmod options permissions filename

There are a number of options, but one of the more frequently used ones is -R, for recursive. But be sure to use that with caution, or you could end up messing things up pretty badly. Don’t use it unless you know you’re in the right place, and that you need to affect the entire directory tree from where you are.

Permissions

The permissions are the most important thing to understand.

There are three parts to file permissions. The Owner, the Group, and Others

chmod 777 filename will give all permissions to everyone – don’t do this! It may make your permission problem go away, but that would be like leaving the vault door open because it’s easier than putting your passcode in to open it!

The first digit sets the permission for the file owner.

The second digit sets the permission for the group.

The third digit sets the permission for others. (Think world here – strangers, people who should not necessarily have access)

If I have file on my server called foo.txt, let’s look at three different ways I might secure it.

chmod 660 foo.txt

This would mean that I as the owner could read and write the file, the group could do the same, and the public (in the case of a webserver) could not access the file at all.

chmod 664 foo.txt

This would mean that I and the group could read and write as before, but now the world can also read.

chmod 644 foo.txt

This would mean that I could read and write, the group can only read, and the public can only read.

So how do the numbers work?

It’s simple actually:

4 = read

2 = write

1 = execute

0 = no permission

So, if I want to read and write, I add 4 + 2 = 6. If I want to write and execute, it’s 2 + 1 = 3, and to read, write, and execute, it’s 4 + 2 + 1 = 7. (That’s why 777 is bad – you are allowing reading, writing, and executing to the public!)

Of course, all this requires that you understand the difference between owner, group, and other, but that will be covered in a different post.