Share this page:

Setting up pretty links in Wordpress

Wordpress URLs can be quite cryptic and hard to remember.  For example, the URL of the sample post in a fresh Wordpress installation is http://192.168.1.50/?p=1, and the URL of the sample page is http://192.168.1.50/?page_id=2. These permalinks can be formatted to make them more human readable using Wordpress's permalink settings so that the sample page's URL is http://192.168.1.50/sample-page/ and the sample post's URL is http://192.168.1.50/hello-world/. These modified URLs are often referred to as pretty links. 

Mod Rewrite

Mod re-write must be enabled in Apache so that pretty links can be used. You can do that by typing this command in a terminal:

sudo a2enmod rewrite

Get the rewrite rules

Apache's configuration must be modified by adding a set of directives that tell Apache how to rewrite URLs for Wordpress. When Apache receives a request with a pretty URL, it must redirect the request to Wordpress's index.php using mod rewrite. When you enable pretty links, Wordpress generates code containing rewrite rules that configures Apache to do this redirection. Login to the Wordpress dashboard, and go to the Permalink page in the Settings menu. Select the format you want, and click on the save changes button. 

Wordpress permalink settings

The rewrite rules will appear at the bottom of the page:

Wordpress rewrite rules

These are the rewrite rules that were generated with the settings I selected:

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

Note that Wordpress displays a message saying that it would write the .htaccess file if it was writeable. For security reasons, it's best not to allow Apache to modify .htaccess files. It's safer to update the file yourself.  

Virtual hosts vs .htaccess files

The rewrite rules can be stored in two different places: your site's Apache virtual host file, or an .htaccess file.

If you enable .htaccess files, then Apache checks to see if a .htaccess file is available every time it receives a request. If there is a .htaccess file, Apache must read the rules that it contains and then parse them. The .htaccess file must be read on every single request handled by the server, which isn't a much of an overhead when a single page is served, but becomes a significant overhead on busy sites. 

You may need to use .htaccess files if you don't have access to the virtual host file, but it's best to avoid using them if possible.  If you can access your server's virtual host file, it's best to save the rewrite code there instead of in .htaccess files. When the rewrite code is placed in the virtual host file, it will be loaded just once when Apache starts up. 

Saving rewrite rules in a virtual host file

To save the code in your virtual host file, open it in an editor with this command:

sudo nano /etc/apache2/sites-available/default

Save the code generated by Wordpress in the Directory directive that defines /var/www:


<VirtualHost *:80>
    ServerAdmin webmaster@localhost

    DocumentRoot /var/www
    <Directory />
        Options FollowSymLinks
        AllowOverride None
    </Directory>
    <Directory /var/www/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride None

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

        Order allow,deny
        allow from all
    </Directory>

    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    <Directory "/usr/lib/cgi-bin">
        AllowOverride None
        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Order allow,deny
        Allow from all
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Type control-o and press return to save changes.  Type control-x to exit nano.

Saving rewrite rules in a .htaccess file

If you don't want to put the code in your virtual file, you can put it in a .htaccess file instead. The virtual host file still needs to be edited to enable the use of .htaccess files. If you can access the virtual host, then open it in nano as described above, and look for the section with the Directory directive that defines /var/www. Change the line 

AllowOverride None

to 

AllowOverride All

The complete virtual host file should look something like this:

<VirtualHost *:80>
    ServerAdmin webmaster@localhost

    DocumentRoot /var/www
    <Directory />
        Options FollowSymLinks
        AllowOverride None
    </Directory>
    <Directory /var/www/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
    </Directory>

    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    <Directory "/usr/lib/cgi-bin">
        AllowOverride None
        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Order allow,deny
        Allow from all
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

If you're using shared hosting, you probably won't have access to the virtual host file, and AllowOverride should already be set to All (if not, talk to your hosting provider).

Now create the .htaccess file in the directory where Wordpress was installed (if you followed this guide on setting up Wordpress on Apache, this will be /var/www/):

sudo nano /var/www/.htaccess

Save the configuration code in the .htaccess file. Type control-o and press return to save changes, and type control-x to exit nano. Make the .htaccess file readable by apache:

sudo chmod 0644 /var/www/.htaccess

Restart Apache

Whether you saved the code in a .htaccess file or in a virtual host file, you need to restart Apache:

sudo service apache2 restart

Now try browsing your site. You should see user friendly URLs in your browser's address bar.


Comments