Fix WordPress Permalinks Not Working in Linux

permalink-not-working-in-linux

When setting up a WordPress site on a Linux server, sometimes the issue permalinks are not working is encountered. Clicking on a post or page link may result in a 404 Not Found error, even though the content exists. This issue is usually caused by incorrect Apache settings, missing modules, or misconfigurations in your WordPress setup.

This guide will walk you through troubleshooting and fixing permalink issues on a Linux-based server.

Step 1: Enable Apache Rewrite Module

WordPress uses the mod_rewrite module to handle permalinks. If it isn’t enabled, permalinks won’t function correctly.

Enable mod_rewrite:

sudo a2enmod rewrite

Restart Apache to apply the change:

sudo systemctl restart apache2

Step 2: Configure Apache Virtual Host for Permalinks

Ensure your Apache configuration allows .htaccess overrides, which WordPress uses for rewriting URLs.

Open your Apache virtual host configuration file.
Example for Ubuntu/Debian:

sudo vim /etc/apache2/sites-available/000-default.conf

Example for Red Hat/Fedora:

sudo nano /etc/httpd/conf.d/wordpress.conf

Look for the <Directory> block and modify it to allow overrides:

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

Step 3: Verify the .htaccess File

Make sure that WordPress’s .htaccess file exists and has the correct rules.

Go to your WordPress root directory:

sudo vim .htaccess
# 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

Ensure the .htaccess file has the correct permissions:

sudo chmod 644 .htaccess

Step 4: Update Permalink Settings in WordPress

  1. Log in to your WordPress admin dashboard.
  2. Go to SettingsPermalinks.
  3. Select the permalink structure you want (e.g., “Post name”).
  4. Click Save Changes to regenerate the .htaccess rules.

If the above steps does not work, do the following steps 

First open up your terminal and type the following command to edit the apach2.conf file.

sudo vim /etc/apache2/apache2.conf

Change the AllowOverride value to ‘All’ from ‘none’ in section as bellow.

<Directory /var/www/>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
  </Directory>
sudo a2enmod rewrite
sudo systemctl restart apache2

That’s it.