WordPress All Pages Not Found Except Homepage

Share this post

WordPress all pages not found except homepage.

WordPress all pages not found except for home page. In this post I’ll cover what causes this problem, why it happens, and how to fix the all pages not found except homepage problem.

If you have this problem, you’ll see a 404 message or a “page not found” message on all site pages, except the homepage.

If you’d like to skip the explanation and would like to do so, you can go straight to the fix by clicking here.


The problem.

Your site’s home page loads OK, but if you try and browse to any other site pages you’ll see a 404 not found error. The 404 not found error is seen when trying to access all site pages, except for the hom page.

Wordpress all pages not found except homepage

What causes this problem.

This problem is caused by the WordPress rewrite rules in your site’s .htaccess either missing or being incorrect.

The .htaccess file should be located in the document root of your site. Files that start with a full stop are hidden files in a Linux environment (which a lot of hosting is). You may need to turn on “view hidden files” to see the .htaccess file.

Before we take a look at the WordPress rewrite rules, do bear in mind that there’s likely to be other things in the .htaccess file as well as WordPress’ rewrite rules. This is because the .htaccess file is used to tell the web server how to serve your site. The other directives you see in this file can include:

  • Directives added by caching plugins.
  • Your site’s security policy.
  •  Directives specifying the version of PHP to be used by your site.
  • Directives added by a security plugin to secure your site.

The reason I mention all this is because if you decide to simply delete your site’s .htaccess file, and then replace it with a new .htaccess file containing only the WordPress rewrite rules, you’re going to be undoing a lot of other config that’s in place by design.

If you do delete and recreate your site’s .htaccess file, it would be strongly advisable to reconfigure any plugins that may have written to the .htaccess file, especially security and caching plugins.


Why a lack of wordpress rewrite rules causes pages to not be found.

Let’s take a look at the standard WordPress rewrite rules. Do be aware that if you’ve moved your site files to a subdirectory of your site’s document root your rewrite rules may differ slightly. The standard WordPress .htaccess rewrite rules look like this:

# 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

What this means to a web server is:

# BEGIN WordPress = This is a comment (just a note) so that we all know it’s WordPress’ rules we’re looking at, and that they start on the next line.

<IfModule mod_rewrite.c> = This line checks if the Apache module mod_rewrite is available. mod_rewrite is used for URL rewriting (URL rewriting is hat most of the rest of the lines do).

RewriteEngine On = Enable the rewriting engine.

RewriteBase / = Sets the base URL path for the rewriting rules. In this case, it’s set to the root directory “/”.

RewriteRule ^index.php$ – [L] = This line is a rewrite rule that matches requests for “index.php” and stops processing any further rules for that request (using the “-” substitution). This rule is used to prevent direct access to the “index.php” file.

RewriteCond %{REQUEST_FILENAME} !-f = This line is a rewrite condition that checks if the requested filename is not an existing file (-f). If the condition is true, it allows the following rewrite rule to be applied. This condition is used to skip existing files from being rewritten.

RewriteCond %{REQUEST_FILENAME} !-d = This line is a rewrite condition that checks if the requested filename is not an existing directory (-d). If the condition is true, it allows the following rewrite rule to be applied. This condition is used to skip existing directories from being rewritten.

RewriteRule . /index.php [L] = This line is the main rewrite rule. It matches any request that hasn’t been skipped by the previous conditions. It rewrites the request to “index.php” and includes the original request as a parameter (e.g., http://example.com/about-us/ will be internally rewritten to http://example.com/index.php with the original request “/about-us/”). The [L] flag indicates that this is the last rule to be processed if it matches.

</IfModule> = This line marks the end of the conditional block for the mod_rewrite module.

# END WordPress = This is a comment (just a note) so that we all know this is the end of the section containing WordPress’ rewrite rules.

Did you get all that? Most people won’t unless you work with things like this all the time. I’ll try and make this a bit simpler.

What the rules above mean in human terms is:

If something’s requested that isn’t a file that exists, and isn’t a directory that exists, rewrite the request to index.php.

At this point you might well be thinking “but my pages exist!”. You’re right, they do, but they don’t exist as a file or within a pages specific directory. They exist in the sites’s database. Say if you try and look in your site files for the file of a specific page, you won’t find it, because you’re looking from something that doesn’t exist.

The best way to think of WordPress is an application as a whole, made of the PHP files in your hosting account, using variables in your site’s database. When a page is called, the PHP in the site files is executed as a whole, this interacts with the database and the two, between them generate page output.

The file that’s used to start the execution of your site’s PHP is the index.php file (the one being redirected to int he rules above), so requests for site pages HAVE to get written back to this index.php file for the PHP execution, database interaction and page output generation to take place.

With the rewrite rules in place, a request for http://example.com/about-us will be rewritten to http://example.com/index.php then the PHP execution and ultimately page output generation takes place.

Without the rewrite rules in place, the web server will handle the request as though your site is made up of pages in files within directories. This means that a request for http://example.com/about-us will be directed to (at file level) public_html/about-us/index.php and this doesn’t exist, so a 404 not found message is displayed.

So why does the home page load?

Good question. Web servers have a directive called DirectoryIndex, which gives the web server a list of default files it should serve when just a directory is requested. This list is usually something like:

index.php, index.html, index.htm, home.htm index.js, index.pl

What this means to a web server is “if just a directory is requested or browsed to look for files in this list and serve the one you find first”.

The home page loads because a request for the home page is just the site’s address, and the site exists in a directory (the document root of your domain) and in this document root there’s the main index.php file of the site. Consequently WordPress’ rewrite rules aren’t required for the site’s home page to load.


How to fix the all pages not found except for home page problem when using wordpress.

You’re going to need to manually edit the site’s .htaccess file using the file manager in your hosting account.

Also, do be aware that this guidance is based on a site being held in the document root of your domain. If you’ve manually moved your site in to a subdirectory of your domain’s document root, you’ll need to take that in to account.

Also, the screen shots used in the following are specific to the cPanel file manager and using the cPanel hosting platform.

The “best practice” way to address this would be to correct the section between “# BEGIN WordPress” and “# END WordPress”, to do this, log in to your hosting account, then click the “File manager” icon:

Open cPanel file manager

Then navigate to the domain’s document root (if you’re using cPanel and have a one domain hosting account this will be public_html, but this may differ if you’re using a different type of hosting or if you have a multisite type hosting account), then right click on the .htaccess file, and select “edit”: 

Edit .htaccess

Then find the “# BEGIN WordPress” and “# END WordPress” section, and delete it (delete what I’ve highlighted in the screen shot below):

Wordpress rewrite rules in .htaccess

..back in to the .htaccess file IN THE SAME PLACE YOU DELETED FROM ABOVE:

Then copy and paste this code…

# 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
paste wordpress rewrite rules

And then save your changes:

Save changes to .htaccess

The “not so best practice” way would be to delete the .htaccess file, then create a new one, add the rewrite rules, then save the file.

If you do decide to do this it’s a good idea to have a look at the .htaccess file before you delete it. When looking at the .htaccess file, make a note of all the comments that start with:

#Start [something]

Because you’re going to need to put all those back. The [something] part of the comment will give you an idea of what plugin in your WordPress you’ll need to reconfigure for it to update the new .htaccess file with it’s plugin specific directives. There may well be multiple plugins in effect that you’ll need to reconfigure so make a list based on all the #Sart directives you see in the .htaccess file.

If you’re using cPanel and you see comments that look like this:

# php — BEGIN cPanel-generate

Or this:

# BEGIN cPanel-generated php ini

You’ll also need to use cPanel’s Multi PHP Manager and Multi PHP ini editor to reselect the version of PHP your site will use and the PHP ini directives you previously had in effect.


If there is no .htaccess file.

If there is no .htaccess file you’ll have to create one in the document root of your domain, edit it, add the wordpress rewrite rules, then save changes.

Obviously you can’t tell what plugins you’ll need to reconfigure without there being an .htaccess file in place to use as reference, but there’s not much you can do about that. It would be prudent to resave all plugin settings, and possibly the cPanel PHP specific directives (using the cPanel Mulit PHP Manager and the cPanel Multi PHP ini editor.

To create the .htaccess file, use the cPanel file manager to navigate to your domain’s document root, then click “+File” toward the top left hand corner:

cPanel file manager create file

Then type:

.htaccess

In the “new file name” field, then click “Create new file”

Create .htaccess file cpanel file manager

Then right click on the .htaccess file and select “Edit”

Edit .htaccess

Then copy and paste this code…

# 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

In to the .htaccess file:

add wordpress rewrite rules to .htaccess

And then save changes:

Save changes to .htaccess file

In conclusion.

  • When using WordPress if all pages show a 404 not found error, except for the home page, this is caused by a lack of WordPress rewrite rules.
  • The WordPress rewrite rules are held in a file called .htaccess.
  • The .htaccess file exists in the document root of a domain.
  • The write rules cause the WordPress application to be invoked when pages are browsed to, by redirecting the request for the page to index.php.
  • WordPress generates the site pages when invoked, by executing PHP, which interacts with the database.
  • The site’s pages exist in the database, not as files within your hosting account.

Share this post

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top