How do I fix weird 404 error

Hi, I have tried everything that I could find on the internet to fix this 404 not found issue, but unable to figure out what's causing the error.

I also checked apache mod_rewrite (enabled), htaccess rules (already set default copy)…

I also checked from the admin area Settings > permalinks, but no luck.

Also checked the user group and user permissions on the newly installed WordPress folders, and they are set correctly to www-data (as that's the user WordPress is using)

I have gone through so many internet pages for about 5 hours but no luck…

10 Replies

It would be helpful if you posted your .htaccess file contents and the site configuration from /etc/apache2/sites-available (assuming Debian/Ubuntu here). Lots of us are very good at solving lots of different kind of problems but none of us have quite mastered mind-reading ;-)

-- sw

.htaccess code

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

mydomain.conf file in /etc/apache2/sites-available

<VirtualHost *:80>
  # Admin email, Server Name (domain name), and any aliases
  ServerAdmin webmaster@mydomain
  ServerName  mydomain.com
  ServerAlias www.mydomain.com

  # Index file and Document Root (where the public files are located)
  DirectoryIndex index.html index.php
  DocumentRoot /var/www/html/mydomain.com/public_html
  # Log file locations
  LogLevel warn
  ErrorLog  /var/www/html/mydomain.com/log/error.log
  CustomLog /var/www/html/mydomain.com/log/access.log combined
RewriteEngine on
RewriteCond %{SERVER_NAME} =mydomain.com [OR]
RewriteCond %{SERVER_NAME} =www.mydomain.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

These lines

RewriteCond %{SERVER_NAME} =mydomain.com [OR]
RewriteCond %{SERVER_NAME} =www.mydomain.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]

permanently redirect http URLs for your site to https URLs. Do you have a virtual host on port 443 set up to be the target for the redirect? If not, that's your problem.

Try commenting out these lines and see if apache2(8) serves your site as http://mydomain.com .

Also

ServerAdmin webmaster@mydomain

should be

ServerAdmin webmaster@mydomain.com

-- sw

Hi Stevewi, thank you reply and for trying to help.

After posting the reply, I also thought it's causing the issue, and I remove the following already and tried, but no help.

[OR]
RewriteCond %{SERVER_NAME} =www.mydomain.com

I am not sure about port 443, but I do have letsencrypt installed on the server and I generated the certificate for this new domain as well.

Fixing the email in serveradmin.

I commented those lines, its still redirecting to https though.

Just so you know, I have another file in sites-available folder which is mydomain.com-le-ssl.conf

and its code is like this

<IfModule mod_ssl.c>
<VirtualHost *:443>
  # Admin email, Server Name (domain name), and any aliases
  ServerAdmin webmaster@mydomain.com
  ServerName  mydomain.com
  # ServerAlias www.mydomain.com

  # Index file and Document Root (where the public files are located)
  DirectoryIndex index.html index.php
  DocumentRoot /var/www/html/mydomain.com/public_html
  # Log file locations
  LogLevel warn
  ErrorLog  /var/www/html/mydomain.com/log/error.log
  CustomLog /var/www/html/mydomain.com/log/access.log combined

Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/mydomain.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/mydomain.com/privkey.pem
</VirtualHost>
</IfModule>

Port 443 is the port for https:

<IfModule mod_ssl.c>
<VirtualHost *:443>

I gather you're using WordPress. Do you have PHP installed correctly for your apache2(8)? Which PHP setup for apache2(8) are you using: mod_php or php-fpm? Is your WP installation at /var/www/html/mydomain.com/public_html? Do you have mod_ssl loaded (do mod_ssl.load & mod_ssl.conf appear as symlinks in /etc/apache2/mods-enabled)?

What I would do is get a sample config working without WP…just serve a simple index.php file that displays 'hello world' or some such:

  • on port 80;
  • on port 443;
  • on port 80 with redirect to 443

Once you get that working, add the WP stuff back in slowly…testing all 3 cases at every step.

-- sw

Okay. I am not that technical.
The Linode server has another WordPress website working perfectly fine on it already and with HTTPS using letsencrypt.

I can test basic PHP running on this new domain too.

and yes, wp installation is in /var/www/html/mydomain.com/public_html

btw, the homepage of WordPress (mydomain.com) also loads perfectly fine. when I click any page or post link, it sends 404 not found.

basically, its permalinks are not working. if I set its default permalink to ?id=123, it works fine that way too.

Okay, this is fixed now, I really appreciate your help.

The solution provided by this guy helped https://stackoverflow.com/a/54791785

Thanks a lot for your help again

btw, the homepage of WordPress (mydomain.com) also loads perfectly fine. when I click any page or post link, it sends 404 not found.
 
basically, its permalinks are not working. if I set its default permalink to ?id=123, it works fine that way too.

Hmmm… definitely sounds like a mod_rewrite problem…

One trick is to turn on the rewrite log. To turn it on,try these lines in your .htaccess or virtual host file:

RewriteLog "/var/log/apache2/rewrite.log"
RewriteLogLevel 3

Since you have two virtual hosts, one for port 80 and one for port 443, I would do it like this:

  • in /etc/apache2/sites-available/mydomain.conf:
<VirtualHost *:80>
   ...
   RewriteLog "/var/log/apache2/rewrite_vhost80.log"
   RewriteLogLevel 3
</VirtualHost>
  • in /etc/apache2/sites-available/mydomain.com-le-ssl.conf:
<VirtualHost *:443>
   ...
   RewriteLog "/var/log/apache2/rewrite_vhost443.log"
   RewriteLogLevel 3
</VirtualHost>

Don’t forget to reload it for your change to take effect:

$ sudo systemctl reload apache2

Hopefully, something will show up in one of those two logs that will be indicative of what is wrong. Don’t forget to turn it off when you are done!

It would also be useful to understand a bit how apache2(8) and mod_rewrite work:

  • this book is the definitive guide to mod_rewrite;
  • there is lots of reference material available about apache2(8)…find a source that is most-useful to you (FWIW, the reference pages have lots of good information but the hurdle for beginners is pretty high…I'd look around at Amazon).

Without access to your site, that's about the best advice I can give at this point. Also, you need to make sure that WP is patched appropriately; all your plugins are patched appropropriately and permalinks are configured correctly (however it is you do all that).

-- sw

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct