An Introduction to WordPress Permalinks
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.
The Default and Ugly Permalinks
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-jeremy
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.
If this is the case, you’ll see a message at the bottom of the Permalinks page, indicating that you must edit the .htaccess
file. If you don’t have a .htaccess
file at the root of your WordPress installation, create it and then copy the contents of the text area WordPress gives you. If this file already exists, append the content displayed on-screen to the file using a text editor.
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-jeremy
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/
.
Editing a Post Slug
If you use the %postname%
tag in your permalinks structure, you can customize even more of your URLs. For each post, you can edit the slug that WordPress generates.
By default, WordPress takes the title and converts it to only use URL-friendly characters. For instance, spaces are converted into dashes, and special characters are also converted, or simply removed: ‘é’ will become ‘e’, ‘à’ will become ‘a’, and ‘?’ or ‘!’ will be removed, for example.
WordPress doesn’t remove small words such as ‘the’ or ‘for’, known as ‘stop words’. These words are not very meaningful in URLs, and you may want to remove them. It’s possible to do that, post by post.
It’s worth mentioning that several SEO plugins automatically take care of removing stop words, so you might already have this functionality on your site. Amit Diwan has a great article reviewing the popular SEO plugins.
Below the Editor’s field for the post title you can read the generated permalink of the post. Next to this link, there’s a button labelled ‘Edit’, which allows you to edit the post permalink.
Once you edit the field with your new slug, hit the ‘Ok’ button. Note that WordPress will ensure that your new URL is valid by creating the slug using the text you entered. If you entered ‘Hello World!’, then you’ll get ‘hello-world’, for example.
Conclusion
Pretty (or almost pretty) permalinks are useful for readable and SEO-friendly URLs. They can contain useful information such as the date of the post or its title. These permalinks are highly customizable and you can choose any structure you want for your website.
Note that you should take your time in choosing a structure. For instance, if you change the structure for your posts, the old URLs will all produce 404 errors which would then all need to be redirected. It’s preferable not to change them for search engines, or people who have shared links to your posts on forums for example.