WordPress Pretty Permalinks

Introduction to WordPress Permalinks

Abd Ur Rehman Permalinks, WordPress Leave a Comment

Website owners typically want readers to share their content either on their own sites or on social networks. To do this, readers need a link to share.

What is a WordPress Permalink?

A ‘permalink’ is a ‘permanent link’ to a specific page or post on a website. When you share a link, you expect that it won’t change and will be a permanent resource.
WordPress generates a permalink for each post or page you create, which can be edited.
There are several types of permalinks that WordPress uses. In this article, I’ll provide an introduction to permalinks, so that you’ll be able to choose the best type for your WordPress site.

Each post or page you create has a unique identifier, called the ID. It’s a number that can uniquely identify your posts or pages, so it’s perfect for a default permalink system, and it’s the one chosen by WordPress.

Let’s assume that you write a post that has the ID 308. Then, its default permalink will be http://example.com/?p=308. By definition, the ID is unique, so by accessing the value of the p tag contained in the URL, WordPress will know which post to retrieve.

It’s a simple system that has a big advantage: it can work on any server, so anyone can use it. However, these permalinks are ugly and not really explicit, and that’s why pretty permalinks exist.

Enabling Pretty Permalinks in WordPress

Pretty permalinks (also known as search friendly URLs) typically contain several words, like the title of the post they refer to. For example, http://example.com/my-blog-post. This way, we can get an understanding of what the content of the page or post will be just by reading the URL. Furthermore, it will also be readable by search engines which in turn can help with search engine rankings.

Choosing the Permalinks Structure

To enable pretty permalinks, go to the Settings > Permalinks menu in your WordPress dashboard. There you’ll find several options available where you can choose for the structure of the permalinks.

The ‘ugly permalinks’ option is the default configuration, with the p parameter. The other options available are ‘pretty permalinks’, you can see in the examples on-screen what information is used. If you don’t like the options WordPress offers, you can even choose the structure you want with the last option ‘Custom Structure’.

You can add any text you want in this structure (but be sure to only use URL-friendly characters!). Some tags allow you to include details like the year of the post, or its title. These tags are called ‘structure tags‘, a term used to describe the variables used in permalinks. Note that this is not to be confused with the standard WordPress ‘tags’ that you use to help categorise your content.

Your structure can specify the date and time of the post. The year of the post is available with the %year%. Include the month with %monthnum% and the day with %day%. The time of the post is available with %hour%, %minute%and %second%.

Two tags allow you to add more information about the post: the category with %category% and the author with %author%. For a post with multiple categories, only one will be included by using %category%: the first one, in alphabetical order.

Finally, we find two important tags: %post_id% and %postname% which refer, respectively, to the post ID and the post title. Note that %postname% doesn’t include the exact post title: instead, it’s the slug which is included, a string representing the title, but only with URL-friendly characters.
If you choose a custom structure, you must include the %post_id% or the %postname% tag or both. Without one of these tags, you can’t be sure that there will be only one post for each URL.

Let’s take an absurdly long example to show what’s possible. To add the date, the time, and the author of a post, we can choose the following structure:

/%year%/%monthnum%/%day%/%hour%:%minute%:%second%/%postname%/written-by-%author%

and we will get URLs like the following one

http://example.com/2016/08/02/08:06:22/hello-world/written-by-abd-ur-rehman

Categories and Tags

Pretty permalinks are not only for your posts. In fact, they are also useful for your categories, tags and archives’ pages. If you choose any other option than ‘ugly permalinks’ for your post URLs, WordPress will automatically enable pretty permalinks for these pages.

You’ll then have URLs like http://example.com/category/uncategorized/ for your categories, instead of http://example.com/?cat=1 by default. For your tags, the tag slug will be prefixed by /tag/. For archives, you’ll get virtual directories like http://example.com/2016/08/ for instance. You can choose the prefix you want for your categories and tags, by filling the fields in the ‘Optional’ section.

Editing the .htaccess

To work, pretty permalinks uses a .htaccess file. This file must be at the root of the WordPress installation. It redirects all the visitors’ queries to the index.php file that interprets them and displays the right page.

If WordPress can edit the .htaccess file itself, all you have to do is hit the ‘Save Changes’ button in Settings > Permalinks to enable pretty permalinks. However, in some cases (which depends on your hosting configuration), WordPress can’t edit it, so you’ll have to do it yourself.

The Almost Pretty Permalinks

There are some cases that disallow you to use pretty permalinks. In fact, if you can’t use .htaccess files, pretty permalinks won’t be available for you. The good news is that it doesn’t mean that you’ll have to use ugly permalinks forever.

WordPress provides an intermediate option: the ‘almost pretty’ permalinks. These permalinks are prefixed by /index.php/. Except for this detail, they are similar to pretty permalinks, giving you the same options and tags. For instance, to use our example above (which is too big) with ‘almost pretty permalinks’, you can fill the ‘Custom Structure’ field with:

/index.php/%year%/%monthnum%/%day%/%hour%:%minute%:%second%/%postname%/written-by-%author%

You’ll then get URLs like:

http://example.com/index.php/2016/08/02/08:06:22/hello-world/written-by-abd-ur-rehman

This is all without the need of a .htaccess file.

As for pretty permalinks, if you enable ‘almost pretty permalinks’, your categories, tags and archives pages will also get almost pretty URLs. Once again, they’ll have the same structure as the pretty permalinks, but prefixed with /index.php/.

If you have any question regarding this post drop me a line in the post.

Leave a Reply

Your email address will not be published. Required fields are marked *