How to Verify and Enable mod_rewrite in Apache for WordPress Permalinks


2 views

After enabling mod_rewrite with a2enmod rewrite, there are multiple reliable ways to verify its activation:

# Method 1: Using apachectl (recommended)
apachectl -M | grep rewrite_module

# Method 2: Checking loaded modules via PHP
<?php
print_r(apache_get_modules());
?>

# Method 3: Parsing the config test output
apache2ctl -t -D DUMP_MODULES 2>&1 | grep rewrite

When you encounter the bad user name ${APACHE_RUN_USER} error, it means you're running the command without proper environment variables. Instead of using apache2 directly, use:

sudo apache2ctl -M

Or check the configuration file directly:

grep -i LoadModule /etc/apache2/mods-enabled/rewrite.load

Create a test file to verify rewrite functionality:

# .htaccess test
RewriteEngine On
RewriteRule ^test-rewrite$ - [L]

# PHP test script (test.php)
<?php
if (in_array('mod_rewrite', apache_get_modules())) {
    echo "mod_rewrite is enabled";
} else {
    echo "mod_rewrite is disabled";
}
?>

After confirming mod_rewrite is active, update WordPress settings:

  1. Go to Settings → Permalinks
  2. Select "Post name" structure
  3. Verify the .htaccess file is writable

Sample working .htaccess for WordPress:

# 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 mod_rewrite appears enabled but isn't working:

# 1. Check AllowOverride setting
sudo nano /etc/apache2/sites-available/000-default.conf
# Ensure: AllowOverride All

# 2. Verify module loading order
# Rewrite modules should load after core modules

# 3. Check for syntax errors
sudo apache2ctl configtest

When working with Apache (especially for WordPress permalinks), confirming mod_rewrite activation is crucial. Here are reliable methods:

The most accurate way to list loaded modules:

apachectl -M | grep rewrite_module

Expected output if enabled:

rewrite_module (shared)

Create a temporary PHP file:

echo "" > /var/www/html/info.php

Then check "Loaded Modules" section in your browser at http://your-server/info.php

Inspect the Apache configuration:

grep -R "LoadModule rewrite_module" /etc/apache2/

Or check symlinks:

ls -l /etc/apache2/mods-enabled/rewrite*

The ${APACHE_RUN_USER} error suggests environment variables aren't loaded. Either use:

sudo -E apache2ctl -M

Or alternatively:

. /etc/apache2/envvars
apache2 -M

Create a .htaccess file with:

RewriteEngine On
RewriteRule ^test$ - [L]

Then visit http://your-site/test. If it loads (instead of 404), rewrite works.

If not active, run:

sudo a2enmod rewrite
sudo systemctl restart apache2