Setting Up Pretty Permalinks in WordPress Using htaccess and mod_rewrite on Linux (CentOS 6)

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

Getting pretty permalinks to work properly on WordPress has turned out to be a much more involving process than what I was expecting, which is a bit annoying. None the less we gotta do what we gotta do.

Requirements

  1. WordPress has been fully installed and configured
  2. Be using Apache Server for your WordPress

We are assuming that you have ssh access to the server with root access.

Locating the PermaLinks Settings in your dashboard

Login to your dashboard by going to http://yourdomain.com/wp-admin. In the left sidebar click on settings and then under settings in the sidebar click on permalinks.

WordPress Dashboard, Settings, PermaLinks

Choose what kind of pretty permalink you want. I went with Post Name. Scroll down to the end of the page and under where it says save changes you should see an interesting message:

If your .htaccess file were writable, we could do this automatically, but it isn’t so these are the mod_rewrite rules you should have in your .htaccess file. Click in the field and press CTRL + a to select all.

That means that our .htaccess file is not writable by wordPress. Time to access the server.

Checking the Existence of the .htaccess file

In your server go to the root directory of the WordPress installation. This is the directory where your index.php file is. If you installed it yourself, then it will have been decided in this step.

[ahmed@amayem ~]$ cd /var/testblog/
[ahmed@amayem testblog]$ ls -a
.                     wp-config.php         wp-login.php
..                    wp-config-sample.php  wp-mail.php
index.php             wp-content            wp-settings.php
wp-activate.php       wp-cron.php           wp-signup.php
wp-admin              wp-includes           wp-trackback.php
wp-blog-header.php    wp-links-opml.php     xmlrpc.php
wp-comments-post.php  wp-load.php

Notice that I had to use the -a flag to check the hidden files (the ones that start with a dot .). There is no .htaccess file here.

Creating the .htaccess file

[ahmed@amayem testblog]$ touch .htaccess
touch: cannot touch `.htaccess': Permission denied

This is to be expected because root is the owner of this directory. Check here to see how to give a linux user sudo powers.

[ahmed@amayem testblog]$ sudo touch .htaccess
[ahmed@amayem testblog]$ ls -a .htaccess 
.htaccess

We have it now, but its permissions are very restrictive:

[ahmed@amayem testblog]$ ls -l .htaccess 
-rw-r--r-- 1 root root 0 Sep  3 15:59 .htaccess

Only root can write to it. If you go back to your dashboard you will still see the same message about .htaccess not being writable.

Modifying .htacces File Privileges and Writing to .htaccess File

[ahmed@amayem testblog]$ sudo chmod 777 .htaccess 
[ahmed@amayem testblog]$ ls -l .htaccess 
-rwxrwxrwx 1 root root 0 Sep  3 15:59 .htaccess

Now anyone can do anything to it, which is very dangerous. We won’t keep it for long, just till WordPress modifies it for us. Now when you go to your dashboard you will see that the message has disappeared. Click on “Save Changes”. Now let’s see the contents of .htaccess.

[ahmed@amayem testblog]$ cat .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

Isn’t that cool. Let’s revert the privileges back now:

[ahmed@amayem testblog]$ sudo chmod 644 .htaccess 
[ahmed@amayem testblog]$ ls -l .htaccess 
-rw-r--r-- 1 root root 236 Sep  3 16:06 .htaccess

Back to normal. Now if we try to access a post using the new pretty permalink we should still get an error page, saying that Apache could not find this url. This is because Apache’s configuration is not allowing the use of .htaccess files.

Enabling .htaccess Files in Apache

We have to change the config of Apache itself. In CentOS 6 this will probably be found as follows:

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

Search by entering /AllowOverride and pressing enter, which should take you to the first instance of the word. In my case it was the following:

# Each directory to which Apache has access can be configured with respect
# to which services and features are allowed and/or disabled in that
# directory (and its subdirectories).
#
# First, we configure the "default" to be a very restrictive set of
# features.
#
<Directory />
    Options FollowSymLinks
    AllowOverride None
</Directory>

Press n to go the next occurrence:

# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
#   Options FileInfo AuthConfig Limit
#
    AllowOverride None

So this is where the issue is, we need to allow Override in the WordPress root directory. So go under </Directory> and let’s enter a new directory, that of the WordPress root (or if you already have the WordPress directory settings here then go to it). Enter insert mode by pressing i.

<Directory "/var/testblog">
    AllowOverride All
</Directory>

Exit insert mode by pressing Esc or Ctrl+c then save and exit by entering :x. Now we need to restart Apache for the changes to take effect.

[ahmed@amayem testblog]$ sudo apachectl restart

Now when we visit our new permalinks we find that they are working.

A Better Way (without htaccess)

Since enabling .htaccess causes a performance hit, and we are revoking WordPress’s priveleges over the file after we modify it anyways, we might as well do it the better and not use .htaccess.

Check Setting up Pretty Permalinks in WordPress With mod_rewrite but Without htaccess

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.