How to Properly Enable mod_rewrite on Amazon Linux for WordPress URL Rewriting


4 views

When configuring Apache on Amazon Linux, many developers encounter situations where mod_rewrite appears to be loaded but doesn't actually function. The key indicators are:

# This command shows compiled modules, not loaded ones
/usr/sbin/httpd -l

The proper way to verify if mod_rewrite is active is:

# Check loaded modules
httpd -M | grep rewrite

For WordPress permalinks to work, three components must align:

# 1. Module loading
LoadModule rewrite_module modules/mod_rewrite.so

# 2. Directory permissions
<Directory "/var/www/vhosts">
    AllowOverride All
</Directory>

# 3. Virtual host configuration
<VirtualHost *:80>
    DocumentRoot /var/www/vhosts/foobar
    <Directory "/var/www/vhosts/foobar">
        Options FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

Many configurations fail because:

  1. The wrong command is used to check module status
  2. AllowOverride is set to None in parent directories
  3. SELinux permissions block .htaccess processing

To verify SELinux status:

# Check current status
getenforce

# If enforcing, add proper context
chcon -R -t httpd_sys_content_t /var/www/vhosts/
chcon -R -t httpd_sys_rw_content_t /var/www/vhosts/foobar/

Here's a verified configuration that works:

# /etc/httpd/conf.modules.d/00-base.conf
LoadModule rewrite_module modules/mod_rewrite.so

# /etc/httpd/conf/httpd.conf
<Directory "/var/www">
    AllowOverride None
</Directory>

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

# /etc/httpd/conf.d/vhost.conf
<VirtualHost *:80>
    ServerName example.com
    DocumentRoot /var/www/vhosts/foobar
    ErrorLog /var/log/httpd/foobar_error.log
    CustomLog /var/log/httpd/foobar_access.log combined
</VirtualHost>

After making changes, always:

# Test configuration
apachectl configtest

# Restart Apache
systemctl restart httpd

If rewrites still don't work:

  1. Check Apache error logs: tail -f /var/log/httpd/error_log
  2. Verify file permissions: ls -la /var/www/vhosts/foobar/.htaccess
  3. Test with a simple rewrite rule first
  4. Disable SELinux temporarily for testing: setenforce 0

When setting up WordPress on Amazon Linux, getting clean permalinks to work requires proper mod_rewrite configuration. Many developers encounter situations where the module appears loaded in configuration but doesn't actually function. Let's break down the complete solution.

First, check if mod_rewrite is actually installed:

httpd -M | grep rewrite

If no output appears, you'll need to install it:

sudo yum install mod_rewrite

Your httpd.conf appears mostly correct, but let's verify these essential elements:

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

For WordPress, your vhost configuration should explicitly allow overrides:

<VirtualHost *:80>
    ServerName foobar.net
    DocumentRoot /var/www/vhosts/foobar/
    <Directory "/var/www/vhosts/foobar">
        Options FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

After making changes, always:

sudo apachectl configtest
sudo service httpd restart

To verify rewrite rules are working, create a test PHP file:

<?php
echo "Rewrite rules are working!";
?>

Ensure proper permissions on .htaccess:

chown apache:apache /var/www/vhosts/foobar/.htaccess
chmod 644 /var/www/vhosts/foobar/.htaccess

On Amazon Linux, SELinux might block .htaccess:

sudo chcon -R -t httpd_sys_content_t /var/www/vhosts/foobar/
sudo setsebool -P httpd_can_network_connect 1

Create a simple test to confirm rewrite is working:

curl -I http://foobar.net/nonexistent-page

Should return 200 OK with your WordPress index.php, not 404.