2019/06/08

Wordpress on Raspberry Pi - Solving the page not found issue when using permalink

This post is about how to solve the page not found problem when using the Permalinks feature of Wordpress.



My Environment

pi@raspberrypi:/ $ lsb_release -a
No LSB modules are available.
Distributor ID: Raspbian
Description:    Raspbian GNU/Linux 9.9 (stretch)
Release:        9.9
Codename:       stretch
pi@raspberrypi:/ $

About the Issue

When using "Post Name" or "Day and name" as the permalink, a not found error occurs when accessing the link in the Wordpress page.

However, this problem doesn't exist when using the "Plain" permalink.


The Solution

After some research and trial & error, I found that the problem is caused by Apache server prohibits using an .htaccess file to apply URL rewrite rules. Below are the steps to enable it.

1. Log in to the LAMP server that has a working Wordpress installed;

2. Change working directory to "/var/www/html";

pi@raspberrypi:/var/www/html $ ls
index.php        wp-blog-header.php    wp-cron.php        wp-mail.php
license.txt      wp-comments-post.php  wp-includes        wp-settings.php
readme.html      wp-config.php         wp-links-opml.php  wp-signup.php
wp-activate.php  wp-config-sample.php  wp-load.php        wp-trackback.php
wp-admin         wp-content            wp-login.php       xmlrpc.php
pi@raspberrypi:/var/www/html $

3. Use the command "cat .htaccess" to check the content of the ".htaccess" file.

Note, an .htaccess file allows us to modify our rewrite rules without accessing server configuration files. For this reason, .htaccess is critical to your web application's security. The period that precedes the filename ensures that the file is hidden.

pi@raspberrypi:/var/www/html $ cat .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 WordPresspi@raspberrypi:/var/www/html $

If the content above is different from the one shown below, replace the above one with the below one.

# 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

Now that we have the ".htaccess" file ready. It's time to enable Apache to use it for URLs rewrite.

4. Enabling mod_rewrite

pi@raspberrypi:/var/www/html $ sudo a2enmod rewrite
Module rewrite already enabled
pi@raspberrypi:/var/www/html $

5. Restart Apache:

Issue "sudo systemctl restart apache2".

6. Check the content of the default Apache configuration file.

pi@raspberrypi:/var/www/html $ cat /etc/apache2/sites-available/000-default.conf
<VirtualHost *:80>
        # The ServerName directive sets the request scheme, hostname and port that
        # the server uses to identify itself. This is used when creating
        # redirection URLs. In the context of virtual hosts, the ServerName
        # specifies what hostname must appear in the request's Host: header to
        # match this virtual host. For the default virtual host (this file) this
        # value is not decisive as it is used as a last resort host regardless.
        # However, you must set it for any further virtual host explicitly.
        #ServerName www.example.com

        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html

        # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
        # error, crit, alert, emerg.
        # It is also possible to configure the loglevel for particular
        # modules, e.g.
        #LogLevel info ssl:warn

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

        # For most configuration files from conf-available/, which are
        # enabled or disabled at a global level, it is possible to
        # include a line for only one particular virtual host. For example the
        # following line enables the CGI configuration for this host only
        # after it has been globally disabled with "a2disconf".
        #Include conf-available/serve-cgi-bin.conf
#RewriteEngine on
#RewriteCond %{SERVER_NAME} =wei48221.ddns.net
#RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
pi@raspberrypi:/var/www/html $

7. Issue "sudo nano /etc/apache2/sites-available/000-default.conf" to add the part highlighted in yellow below to the default Apache configuration file. Make sure that all blocks are properly indented.

Save and exit the editor when done.

pi@raspberrypi:/var/www/html $ cat /etc/apache2/sites-available/000-default.conf<VirtualHost *:80>
    <Directory /var/www/html>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

        # The ServerName directive sets the request scheme, hostname and port that
        # the server uses to identify itself. This is used when creating
        # redirection URLs. In the context of virtual hosts, the ServerName
        # specifies what hostname must appear in the request's Host: header to
        # match this virtual host. For the default virtual host (this file) this
        # value is not decisive as it is used as a last resort host regardless.
        # However, you must set it for any further virtual host explicitly.
        #ServerName www.example.com

        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html

        # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
        # error, crit, alert, emerg.
        # It is also possible to configure the loglevel for particular
        # modules, e.g.
        #LogLevel info ssl:warn

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

        # For most configuration files from conf-available/, which are
        # enabled or disabled at a global level, it is possible to
        # include a line for only one particular virtual host. For example the
        # following line enables the CGI configuration for this host only
        # after it has been globally disabled with "a2disconf".
        #Include conf-available/serve-cgi-bin.conf
#RewriteEngine on
#RewriteCond %{SERVER_NAME} =wei48221.ddns.net
#RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
pi@raspberrypi:/var/www/html $

8. Check the configuration

pi@raspberrypi:/var/www/html $ sudo apache2ctl configtest
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message
Syntax OK
pi@raspberrypi:/var/www/html $

If there are no errors, restart Apache to put your changes into effect:

sudo systemctl restart apache2

9. Go back to Wordpress admin console. Enable the use of "Post name" under permalinks and test this setting out again.

Yes. It's working now!!


References:

How to Fix WordPress Posts Returning 404 Error
https://www.wpbeginner.com/wp-tutorials/how-to-fix-wordpress-posts-returning-404-error/

How To Rewrite URLs with mod_rewrite for Apache on Debian 9
https://www.digitalocean.com/community/tutorials/how-to-rewrite-urls-with-mod-rewrite-for-apache-on-debian-9

Check Raspbian Version
https://www.unixtutorial.org/check-raspbian-version

No comments:

Post a Comment