WordPress URL rewrite rules need to be configured based on your server environment (Apache or Nginx). This guide explains the standard configuration methods for both major web servers and notes considerations for installations in subdirectories.
What URL Rewrite Rules Do
URL rewriting (often called 'pretty permalinks') transforms dynamic URLs (like example.com/?p=123) into cleaner, more readable static-like forms (like example.com/post-name). This improves SEO and user experience. Before configuring, ensure your WordPress Settings → Permalinks is set to a non-default option (e.g., 'Post name').
Apache Configuration
Apache typically uses a .htaccess file to manage rewrite rules.
Step 1: Enable the Required Module
In your main Apache configuration file (httpd.conf or apache2.conf), ensure the rewrite module is loaded (not commented out):
LoadModule rewrite_module modules/mod_rewrite.so
Also, in the configuration for your website's directory, change AllowOverride None to AllowOverride All to allow .htaccess rules to take effect.
Step 2: Create the .htaccess File
In your WordPress installation's root directory, create or edit the .htaccess file (note the leading dot). Insert the following standard rules:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Explanation: These rules first check if the requested file or directory exists. If not, the request is forwarded to index.php for WordPress to handle. If WordPress is installed in a subdirectory (e.g., /blog/), simply change RewriteBase / to RewriteBase /blog/.
Nginx Configuration
Nginx configuration is written directly into a server block configuration file, typically located in /etc/nginx/conf.d/ or /etc/nginx/sites-available/. After editing, reload Nginx (nginx -s reload).
Root Installation Rules
Inside your server { ... } block, add the following rule within the location / context:
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
This is the recommended standard method for WordPress on Nginx, as it is more efficient and secure than complex if conditionals.
Subdirectory Installation Rules
If WordPress is installed in a subdirectory (e.g., /wp/), configure a specific location block for it:
location /wp/ {
try_files $uri $uri/ /wp/index.php$is_args$args;
}
Replace /wp/ with your actual subdirectory name.
Troubleshooting & Checks
- File Permissions: Ensure the
.htaccessfile (Apache) is readable by the web server. - Configuration Reload: After modifying Nginx config, always test syntax with
nginx -tbefore reloading withnginx -s reload. - Control Panels: If using a server panel like cPanel, Plesk, or others, they often have a 'URL Rewrite' or 'Permalinks' section where you can select 'WordPress' for automatic configuration.
- Flush Rules: After configuring your server, visit WordPress Admin → Settings → Permalinks and click 'Save Changes' to refresh the internal rewrite rules.
Note: These rules are compatible with current WordPress versions and Apache 2.4+/Nginx 1.18+. If issues persist, check your server's error log for specific clues.