Question

WordPress permalinks broken on Ubuntu 20.04 LAMP stack

Posted July 18, 2020 4.2k views
ApacheWordPressUbuntu 20.04

Getting the following 404 error message:

Not Found
The requested URL was not found on this server.
Apache/2.4.41 (Ubuntu) Server at matbury.com Port 443

So far on my VPS:
Let’s encrypt certbot installed & HTTPS set at default (redirect)
Apache mod_rewrite installed & enabled

Allow Apache to write WordPress’ .htaccess file:

$chown www-data:www-data /var/www/html/wordpress/.htaccess
$chmod 755 /var/www/html/wordpress/.htaccess

$sudo nano /etc/apache2/sites-available/000-default.conf
<VirtualHost *:80>
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html

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

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

(I even tried changing the port number to 443)

$sudo nano /etc/apache2/apache2.conf
ServerName localhost
$sudo service apache2 restart

(Even tried rebooting the server)

$sudo nano /var/www/html/wordpress/wp-config.php
Added:
define( 'WP_SITEURL', 'https://matbury.com/wordpress' );

$sudo nano /var/www/html/wordpress/.htaccess
# BEGIN WordPress
# The directives (lines) between `BEGIN WordPress` and `END WordPress` are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /wordpress/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wordpress/index.php [L]
</IfModule>

# END WordPress

I also have Moodle 3.9 running on the same server. No problems with that but it doesn’t use mod_rewrite.

Everything works fine with Plain links set in WordPress. When I switch to permalinks I get the 404 error. What am I missing or doing wrong?

Thanks in advance! :)

edited by MattIPv4

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
Submit an Answer
5 answers

Hi there @matbury,

As your WordPress installation is stored at /var/www/html/wordpress you would need to add another AllowOverride rule for that folder as well:

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

That way the .htaccess file in your /var/www/html/wordpress folder would take effect.

After that run an Apache config test and if you get Syntax OK restart it.

Let me know how it goes.
Regards,
Bobby

Thanks for responding bobbyiliev :)

Where should I add the additional AllowOverride rule? I’ve tried this:

<VirtualHost *:80>
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html

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

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

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

in /etc/apache2/sites-available/000-default.conf

I’ve also tried with a trailing slash on <Directory /var/www/html/wordpress/>

Syntax is OK but still getting the 404 error.

Please help.

  • Hi there @matbury,

    This actually looks correct I believe. I also tested this at my end, it should actually work without the extra AllowOverride rule that I suggested.

    Can you verify that mod_rewrite is enabled:

    • sudo apachectl -M | grep rewrite_module

    Also are there any errors in your Apache error log?

    • sudo tail -100 /var/log/apache/error.log

    Regards,
    Bobby

Hi,

I checked again & mod_rewrite is enabled.

Apart from bots trying to access non-existent pages & this error:

PHP Fatal error: Uncaught Error: Call to undefined function get_header() in /var/www/html/wordpress/wp-content/themes/twentyseventeen/404.php:13\nStack trace:\n#0 {main}\n thrown in /var/www/html/wordpress/wp-content/themes/twentyseventeen/404.php on line 13

I couldn’t see any other errors in the Apache logs.

I’ve also tried the twentytwenty theme just in case.

Here’s the Wordpress generated .htaccess file:

# BEGIN WordPress
# The directives (lines) between `BEGIN WordPress` and `END WordPress` are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /wordpress/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wordpress/index.php [L]
</IfModule>

# END WordPress

What else can I try?

Thanks in advance! :)

  • Hi,

    I have same configuration. Please try this, it works on my server.

    In this file: /etc/apache2/sites-available/000-default.conf add these lines at the end of file, not in <virtualhost> tag!

    <Directory "/var/www/yourdomain">
        AllowOverride All
    </Directory>
    

Also - a little gotcha in your original post… you probably figured it out already, but this will help others…

Invalid XML tag

You had:
<Directory /var/www/html/>
Options Indexes FollowSymLinks
AllowOverride all
Require all granted
</Directory>

Note the open tag for ‘Directory’ should not have the ending ’/>’

It shoud just be

<Directory /var/www/html>


</Directory>

So anyone copying this, replacing html with their own website name, will have the same issue if not caught.

Cheers

Hi All,

Problem: wordpress site was broken and rewrite was not working

I reviewed vhost *.conf and was fine
I reviewed .htaccess and was fine

so, what was wrong???

I have many wordpress sites in 1 droplet, and all sites was wrong

after try several times this worked for me

sudo nano /etc/apache2/apache2.conf

insert:

ServerName localhost

and then restart apache

sudo systemctl reload apache2

and

sudo a2ensite example.conf
sudo service apache2 restart