Setting Up Pretty Permalinks in WordPress WITHOUT htaccess on Linux (CentOS 6)

This post is part of a step-by-step series on installing WordPress on Linux (CentOS 6)

As shown here, when we setup pretty permalinks using .htaccess we end up having to do it manually. It is also mentioned by Apache that enabling .htaccess causes a performance hit, so why not just configure Apache properly to beging with and avoid the performance hit?

You should avoid using .htaccess files completely if you have access to httpd main server config file. Using .htaccess files slows down your Apache http server. Any directive that you can include in a .htaccess file is better set in a Directory block, as it will have the same effect with better performance.

Requirements

  1. WordPress has been fully installed and configured
  2. Be using Apache Server for your WordPress
  3. Have an understanding of Setting up Pretty Permalinks using .htaccess
  4. root access to your server

Moving the Contents of .htaccess to httpd.conf.

Basically we are going to move the contents of the .htaccess file in the WordPress root directory and put it in the httpd.conf file which dictates all the configuration for Apache itself. First let’s get the contents of .htaccess:

[ahmed@amayem ~]$ cat /var/testblog/.htaccess 

# 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

This is what I got when I setup the file earlier. Copy that content. Now let’s open httpd.conf for editing:

[ahmed@amayem ~]$ sudo vi /etc/httpd/conf/httpd.conf 

Enter the following to search for the directory specific configurations

/<Directory

The first one will probably be the following:

<Directory />
    Options FollowSymLinks
    AllowOverride None
</Directory>

Press n to go the next occurrence. If you find one that looks like this:

<Directory "/path/to/your/blog/root">

Then you are in luck and just need to paste the contents under that, but make sure you change AllowOverride to None if it is there or else add it with None. If you can’t find it, then find and paste the following under it:

<Directory "/var/testblog">
    AllowOverride None
    # 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
</Directory>

Make sure that AllowOverride None is there, because that is what is telling Apache to not use .htaccess.

Exit insert mode using Esc or ctrl+c then exit and save by entering :x.

Now we need to restart apache:

[ahmed@amayem ~]$ sudo apachectl restart

Check one of your pretty permalinked posts now, and it should be working like a charm.

Deleting the old .htaccess File

[ahmed@amayem ~]$ sudo rm /var/testblog/.htaccess

Done! Congratulations!

References

  1. WordPress Official Codex on Using Pretty Permalinks
  2. Apache’s htaccess howto

Ahmed Amayem has written 90 articles

A Web Application Developer Entrepreneur.