Tiny WordPress Insights

Moving WP .htaccess rules to httpd.conf

Recently, in one of my clients’ server, we decided to move to the traditional Apache with mod_php instead of Nginx with php-fpm, after going through the pros and cons of each setup. If you are not aware already that AllowOverride All brings a major performance lag into the whole setup. To overcome this, we can move the rewrite rules written on any part of your site into Apache’s configuration file (apache.conf or httpd.conf or any other name depending on the distribution you use). By moving the rewrite rules into the httpd.conf, we could disable AllowOverride altogether for a particular site and make this Apache with mod_php server stack a bit faster than Nginx with php-fpm stack. Here is how you can move all the rewrite rules into your server’s config…

Combining Apache VirtulHost and WordPress Permalink Rewrite Rules

Here is the traditional virtual host configuration in Apache…


    ServerAdmin  DocumentRoot /www/docs/dummy-host.example.com ServerName dummy-host.example.com ErrorLog logs/dummy-host.example.com-error_log CustomLog logs/dummy-host.example.com-access_log common

And here is the traditional rewrite rules for pretty permalinks


# BEGIN WordPress

RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

# END WordPress

So, let’s combine this…


    ServerAdmin  DocumentRoot /www/docs/dummy-host.example.com ServerName dummy-host.example.com ErrorLog logs/dummy-host.example.com-error_log CustomLog logs/dummy-host.example.com-access_log common # BEGIN WordPress RewriteEngine On RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] # END WordPress

Finally, lets test the configuration and reload the Apache server…

httpd -t && service httpd reload
> Syntax error on line 1037 of /etc/httpd/conf/httpd.conf:
> RewriteBase: only valid in per-directory config files

Oops. :) It turns out that we have to use “Directory” to insert the rewrite rules. So, here is the modified version…


    ServerAdmin  DocumentRoot /www/docs/dummy-host.example.com ServerName dummy-host.example.com ErrorLog logs/dummy-host.example.com-error_log CustomLog logs/dummy-host.example.com-access_log common <Directory "/www/docs/dummy-host.example.com"> # BEGIN WordPress RewriteEngine On RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] # END WordPress </Directory>

Let’s see, if we could succeed this time…

httpd -t && service httpd reload
> Syntax OK
> Reloading httpd: [OK]

That’s it. Now, you can be proud to say that your Apache server with mod_php is faster than Nginx with php-fpm.

Let’s do more: Akismet .htaccess code in httpd.conf

Since version 2.5.7, Akismet introduced a new .htaccess file to block direct access to files. Let’s dive in and see what’s in it…


Order Deny,Allow
Deny from all

<FilesMatch "^akismet\.(css|js)$">
    Allow from all
</FilesMatch>

Pretty simple? Yeh. Let’s put it in Apache config file…


    ServerAdmin  DocumentRoot /www/docs/dummy-host.example.com ServerName dummy-host.example.com ErrorLog logs/dummy-host.example.com-error_log CustomLog logs/dummy-host.example.com-access_log common <Directory "/www/docs/dummy-host.example.com"> # BEGIN WordPress RewriteEngine On RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] # END WordPress </Directory> <Directory "/www/docs/dummy-host.example.com/wp-content/plugins/akismet"> Order Deny,Allow Deny from all <FilesMatch "^akismet\.(css|js)$"> Allow from all </FilesMatch> </Directory>

Final Stack

We deployed Nginx to serve the static files and Varnish to cache the dynamic content. We were still not satisfied with the performance of our server. Upon troubleshooting, we noticed that we had forgotten to remove AllowOverride All. :)

Exit mobile version