If you’re running WordPress on an Amazon EC2 instance with Amazon Linux and Apache, and your inner pages (posts, custom pages, etc.) are returning 404 errors, don’t panic! It’s a common issue related to Apache rewrite rules and .htaccess.

Here’s a step-by-step guide to fix it properly.

✅ Symptoms

  • Home page works fine
  • Clicking on blog posts, pages, or custom post types shows a 404 Not Found
  • Pretty permalinks (/sample-page/) break, but plain URLs (?p=123) work

🛠 Root Cause

Apache is likely not allowing .htaccess overrides, which WordPress needs to handle URL rewrites using mod_rewrite.

✅ Step-by-Step Fix on Amazon EC2 (Amazon Linux + Apache)

1️⃣ Edit Apache Configuration

Open your Apache config file:

sudo nano /etc/httpd/conf/httpd.conf

Find this section:

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

Change AllowOverride None to AllowOverride All:

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

Press Ctrl + O to save, Enter to confirm, then Ctrl + X to exit.

2️⃣ Restart Apache

Apply your changes by restarting Apache:

sudo systemctl restart httpd

3️⃣ Resave Permalink Settings in WordPress

Now, tell WordPress to regenerate its rewrite rules:

  1. Go to your WordPress Admin Dashboard
  2. Navigate to Settings → Permalinks
  3. Click Save Changes (even if you don’t change anything)

This will trigger WordPress to flush and rewrite the .htaccess file.

4️⃣ Verify Your .htaccess File

Ensure that the file /var/www/html/.htaccess exists and contains:

# 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

If it’s missing, create it manually and set correct permissions:

sudo nano /var/www/html/.htaccess

Paste the content above, save, then run:

sudo chown apache:apache /var/www/html/.htaccess
sudo chmod 644 /var/www/html/.htaccess

✅ Success! Your Inner Pages Should Now Work

Visit your WordPress site and try opening inner pages like:

https://yourdomain.com/sample-page/

You should no longer see 404 errors.

🧠 Bonus: Confirm mod_rewrite Is Enabled

Run this to ensure the rewrite module is active:

httpd -M | grep rewrite

You should see:

rewrite_module (shared)

If it’s not enabled, load it manually in your config or module directory, then restart Apache.