Back to blog

A permalink is a permanent URL to each post. It should never be changed — hence permalink.  Those are the URLs to your individual posts on weblog, as well as on other lists of weblog postings and categories. For example, a permalink is a link to your article that you can send via e-mail.

use-permalinks-in-wordpress

WordPress permalinks have three basic types

Types of Permalinks

Default: “Very Ugly”

The default permalink looks like this:

http://sample.com/?p=X

where X is the number of Post ID. It does not look as nice as some of the other options, however, it works on all server environments.

mod_rewrite: “Pretty Permalinks”

When you use lighttpd or mod_rewrite, you can generate much prettier permalinks. The most common and the most versatile permalinks look like the following, however, there are many different formats:

http://sample.com/2012/post-title/

or

http://sample.com/2012/12/30/post-title

You can get pretty permalinks under:

  • Lighttpd using a mod_rewrite or 404 handler
  • Apache web server with the mod_rewrite module
  • Microsoft IIS 7+ web server with PHP 5 running as FastCGI and the URL Rewrite 1.1+ module
  • Hiawatha web server with enabled UrlToolkit support.
  • Microsoft IIS 6+ using Ionic ISAPI Rewriting Filter (IIRF) (free for multi-site or single-site server)
  • Microsoft IIS 6+ using ASAPI_Rewrite (free for single-site server, $$ for multi-site server)

PATHINFO: “Almost Pretty”

For one exception, PATHINFO permalinks look like mod_rewrite permalinks. They have /index.php in the beginning:

http://sample.com/index.php/yyyy/mm/dd/post-title/

They are similarly flexible as the “Pretty” mod_rewrite permalinks. With the help of that /index.php part, PATHINFO permalinks can do anything that mod_rewrite permalinks.

Choose the structure of your permalink

You can enter your own structure or choose one of the “common” structures in the “Custom structure” field using the structure tags in the Settings → Permalinks panel.

Pay attention: You must use a combination of tags or one of the structure tags only. Never put your site`s url in the permalinks slot.

Start your permalink structure with index.php/ to activate PATHINFO permalinks.

Structure Tags

To customize your “Nice” or “Almost Nice” permalinks, see a few tips:

Make sure you end your structure with either %postname% or %post_id% (e.g. /%year%/%monthnum%/%day%/%post_id%/). Each permalink should point to a separate post.

%year%

The year of the post, four digits. For instance, 2014

%monthnum%

Month of the year. For instance, 12

%day%

Day of the month. For instance, 03

%hour%

Hour of the day. For instance, 16

%minute%

Minute of the hour. For instance, 57

%second%

Second of the minute. For instance, 43

%post_id%

The unique post ID number. For instance, 178

%postname%

The post`s title sanitized version (post slug field on Page panel/Edit Post). Thus, “It Is A Nice Post” becomes it-is-a-nice-post in the URL.

%category%

The category`s name sanitized version (category slug field on Edit Category/New panel). Nested directories in the URL appear as Nested sub-categories.

%author%

The the author`s name sanitized version.

Tag base and Category base

The Tag base and Category base are prefixes used in tag archives and URLs for category. They look as follows:

sample.net/wp/category_base/category_title
sample.net/wp/tag_base/tag_title

Tag and category are the default values for these. You cannot remove them from the URLs altogether, however, you can always change them.

Generally, custom permalinks are compatible with the most systems.

How to use %category% with multiple categories on a post

Only one category will be displayed in the permalink when you assign multiple categories to a post.

The sub-category group are ordered in alphabetical order. The same applies to categories. Through all the categories, you will have an access to all posts as usual.

How to use “Pretty” permalinks

What is needed:

  • Installed apache web server with the mod_rewrite module;
  • In WordPress’s home directory, you should enable the FollowSymLinks option and allow FileInfo directives (for example, AllowOverride All or AllowOverride FileInfo). Make sure you have an .htaccess file (WordPress will try to create it when you activate “pretty” permalinks, if this file is missing). WordPress must have access to the file, if you want WordPress to update the .htaccess file automatically.
  • Add the following location block within the server block for nginx, a web server aimed at low memory usage, high concurrency and high performance:

location / {
           try_files $uri $uri/ /index.php?$args;
}

  • Use the following UrlToolkit rule for Hiawatha, a web server with a strong focus on security:

UrlToolkit {
   ToolkitID = wordpress
   RequestURI exists Return
   Match .*\?(.*) Rewrite /index.php?$1
   Match .* Rewrite /index.php
}

  • Macintosh Users should edit the AllowOverride line in the httpd.conf file to read AllowOverride All in the Directory “/Library/WebServer/Documents”. Usually, it is located at /etc/httpd/httpd.conf. For Mac OS X 10.5.x and up, this file is located in /private/etc/apache2/users/[your-username].conf.

WordPress will generate rewrite rules and attempt to insert them into the proper .htaccess file every time you update or create a “pretty” permalink structure. In case of failure, you will see a notification like  “You should update your .htaccess now and print out the rules for you to copy and paste into the file “.

WordPress will not delete any existing Rewrite Rules or other directives, and will play nice with an existing .htaccess file. Put yours mod_rewrite rules before WordPress’s if you have another one.

Where can you find the .htaccess file?

WordPress’ .htaccess and index.php files should both be  located on your General Options page in the directory indicated by the Site address (URL) option. You may need to change the preferences of the FTP tool to show all files, including the hidden files. Since the name of the file begins with a dot, the file may not be visible through an FTP client. If you install WordPress through the Godaddy Hosting Connection installation, some hosts may not show or allow you to edit .htaccess (e.g. Godaddy).

Editing or creating (.htaccess)

Create an .htaccess file if you do not already have one. A simple command touch .htaccess will create the file if you have shell or ssh access to the server. Create a file on your local computer, name it 2.htaccess, upload it to your WordPress folder`s root. Then rename it to .htaccess if you prefer to use FTP to transfer files. You can edit the .htaccess file by your host’s control panel, FTP or shell. In WordPress 3.0+, the following permalink rewrite code should be included in your .htaccess file:

# 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

You will need to use your host’s control panel or FTP to delete the rogue .htaccess file if your .htaccess file contains errors that bring down your site (like “Internal Server Error (500)”).

How to update .htaccess automatically

Firstly, you will need to give WordPress access to the .htaccess file. Try giving permissions to the owner, then to the group, then world. It is suggested to test it each time you make any changes; don’t add any further write permissions once WordPress has edited the file successfully. You can get all the necessary access permissions on your server setup page. To prevent other users from potentially having access to the server, you should create stronger permissions after applying the permalinks.

Using Permalinks without mod_rewrite

IIS (common on Windows servers) does not support mod_rewrite and “Pretty” permalinks, and usually requires mod_rewrite. Instead, you can use Microsoft’s URL Rewrite Module if you have admin rights on your server and use IIS 7.  It supports WordPress’ pretty permalinks, since it is not completely compatible with mod_rewrite. After the installation, add the following rule to the system.webServer element in the web.config file:

<?xml version=”1.0″ encoding=”UTF-8″?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name=”WordPress Rule” stopProcessing=”true”>
<match url=”.*” />
<conditions>
<add input=”{REQUEST_FILENAME}” matchType=”IsFile” negate=”true” />
<add input=”{REQUEST_FILENAME}” matchType=”IsDirectory” negate=”true” />
</conditions>
<action type=”Rewrite” url=”index.php?page_id={R:0}” />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

You can always try PATHINFO permalinks if it doesn’t work; just add index.php/ at the beginning of your custom permalink structure:

/index.php/%year%/%monthnum%/%day%/%postname%/

If WordPress runs on IIS 6, this option may not always work. Add these two lines to a php.ini file and store that file in your webroot to make this option work on IIS:

cgi.fix_pathinfo = 1
cgi.force_redirect = 0

Using IIS’ custom 404 redirects is another solution. Your permalink structure should not begin with /index.php/, as well as you shouldn’t  install any 3rd party mod_rewrite software. However, your web host allows you to add a custom 404 redirect.

How to Fix Permalink Problems

How to Fix .htaccess Generation Issues

There is a couple reasons that may make your WordPress installation stop writing the new rules onto your existing .htaccess file or does not generate a .htaccess file at all. Follow the instructions below step by step. If the previous step does not work, go to the next step.

      1. Change Permissions of Files: It is not recommended to chmod the .htaccess file to 666 or to edit it with WordPress template editor, since all your blog`s users will be able to edit it as well. To make it server-writable, you can change the permissions to 660, but the limitation will remain the same.
      2. Blockage of the Server: WordPress’ .htaccess generation may fail since the SERVER_SOFTWARE is blocked by the host. By changing your wp-includes/vars.php file, you can force WordPress to believe that your server is running Apache. To implement these changes, follow the steps below. Using the built-in file editor in your WordPress Admin panel, open the wp-includes/vars.php file. Login to WordPress, click “Manage”, then click “Files”, scroll to the bottom and write in wp-includes/vars.php into the text box under the “Other Files” title, to navigate to this panel. Find and type // $is_apache = strstr($_SERVER[‘SERVER_SOFTWARE’], ‘Apache’) ? 1 : 0 instead of $is_apache = strstr($_SERVER[‘SERVER_SOFTWARE’], ‘Apache’) ? 1 : 0 and type in $is_apache = 1 and add it as a new line under // $is_apache = strstr($_SERVER[‘SERVER_SOFTWARE’], ‘Apache’) ? 1 : 0;
      3. XAMPP Users (for Windows): Some XAMPP versions by default do not provide mod_rewrite (since it is compiled in Apache). To create pretty permalinks, you should open apache/conf/httpd.conf and uncomment the line LoadModule rewrite_module modules/mod_rewrite.so to make it enabled, and hence enable WordPress to write the .htaccess file.
      4. WAMP Users (for Windows): Some WAMP versions by default do not permit following SymLinks or provide mod_rewrite.  Firstly, using a text editor, uncomment the line LoadModule rewrite_module modules/mod_rewrite.so to let the required functionality navigate to the apache/conf/httpd.conf file. Then change the second line in that section from “AllowOverride none” to AllowOverride all function in the same file in a section that starts with the line “Options FollowSymlinks”. Your permalinks should start working right after you edited httpd.conf and restart all WAMP modules.
MS Frontpage, .htaccess and Permalinks

Some words about Microsoft Frontpage: most servers (dedicated and shared) are built and maintained by different hosting companies, with the Apache built-in compiled with mod_frontpage, and often installed Frontpage Server Extensions on every virtual server. These days, most hosting companies in the server build process include both the server extensions and mod_fronpage. It is common for most binary distributions. Although the admin panel may operate correctly, you would likely get something like a blank white page or 500 error even if you’re not using Frontpage. It may occur when trying to view your WP install, simply because extensions/mod_frontpage exist on your server, as a result of the way the extensions interact with the httpd.conf file and Apache.

WordPress works correctly with the installed Frontpage Extensions, however ANY changes to the permalinks section from the WordPress admin interface will cause the Frontpage server extensions` corruption since permalinks will not function at all due to the addition of the mod_rewrite rules to the .htaccess file.

Permalinks, Frontpage or Quick Fixes

Fixing Frontpage Extensions: If you just want to make the MS Frontpage server extensions work again and don’t need permalinks anymore, simply remove the WordPress section with the rewrite rules from your your .htaccess file.

Using Permalinks: If your hosting company has the installed extensions, and you don’t care about Frontpage, you will need to edit the .htaccess file and all of the Frontpage Lines, leaving only the WordPress mod_rewrite code or just delete the MS Frontpage server extensions.

Using Permalinks and FrontPage Together

Recommended solution

Until now, there was a number of threads on the support forums on this issue, yet no real solution to the problem.

Traditionally, with the installed Microsoft FrontPage Server extensions on a Unix server WordPress, it operates just fine, and you are able to publish and edit pages with Microsoft FrontPage. Unless you change the permalinks (for e.g the date of birth /1992/05/etc).

Recently, the .htaccess file is used by the FrontPage for its “web authoring” and “publishing” configuration. So the Frontpage Server extensions become corrupt and the permalinks will stop working as soon as the WordPress mod_rewrite code is added to the file.

Note: it is practically impossible to allow using FrontPage and management at the same time and use permalinks.

However, the solution was found by accident. It is actually quite simple.

If you are using FrontPage (or if you have a pre-configured hosting package) along with WordPress, you will need to follow the next steps on your server.

Create the following directory in Microsoft FrontPage

_vti_bin

Nested within that it creates both

_vti_aut  and  _vti_adm

You will find additional .htaccess files in all those directories in addition to your site (or WordPress) root folder.

You simply need to add one line at the top of ALL of the .htaccess files in all three of these directories as well as in the root directory:

Options +FollowSymlinks

They may have a line in each like

Options None

Now you should edit and save each .htaccess file. That is all. Everything will work perfectly, including the permalinks of your choosing and the FrontPage.

What is Long Permalinks

Some long permalinks are “chopped off” when only the first section is actually recognized as text when using extra long permalinks in posting and email in chats and comments. For example:

http://mydomain.sample.com/2002/13/7/post-about-elvis-presley-and-a-spoon

Can result in:

http://mydomain.sample.com/2002/13/7/post-about-elvis-presley-and-a-spoon

The user would get a 404 Page Not Found Error when clicking on the lower link. Follow these steps to prevent this problem, if you have a tendency to use very long permalink post titles.

  1. Make sure you use Permalinks.
  2. Add the following in your .htaccess file:

RewriteRule ^post/([0-9]+)?/?([0-9]+)?/?$ /index.php?p=$1&page=$2 [QSA]

3. It is time for testing. Type the following (your information) in a post ID number and you will be redirected to your post:

http://mydomain.sample.com/post/(the ID #)

Also, when pasting URLs into emails, please note that the majority of email services will not cut off URLs that have been delineated with angle-brackets (< and >). So, you should type them the following way:

Please, visit my blog at <http://mydomain.sample.com/2002/13/7/post-about-elvis-presley-and-a-spoon>

In addition, when composing plain-text emails, some decent email clients offer a “preformat” option. Do not insert linebreaks inside the links when pasting links will force the email client and using this “preformat” option.

How to Fix Other Issues

It can be a problem if Permalinks do not function while your .htaccess file is being generated correctly. If so, post a note on the WordPress Forum.

Not Enabled AllowOverride

Your server may have disabled the AllowOverride directive. The .htaccess files are completely ignored in your Apache httpd.config file if the AllowOverride directive is set to None. The server will not even try to read .htaccess files in the file system in this case. Any directive which has the .htaccess Context is allowed in .htaccess files if this directive is set to All. Example of the httpd.config file with enabled AllowOverride directive:

<Directory />
Options FollowSymLinks
AllowOverride All
</Directory>

In your DocumentRoot, you may also need to enable the AllowOverride directive:

<Directory /var/www/html>
# … other directives…
AllowOverride All
</Directory>

It may also be necessary to change the AllowOverride settings for the site. We are talking about Mac OS X Server, but the same may applies to other systems. The site configuration files can be usually found in /etc/httpd/sites/

Your AllowOverride list must include the FileInfo directive if you don’t want to set AllowOverride to all (as it is above). For any httpd.config file changes to take effect, you must restart your Apache server.

Problem with Paged Navigation

Sometimes it may be a problem with a navigation to second (and subsequent) posts or pages. Your page may have one of these URIs:

http://www.sample.com/page/3/
http://www.sample.name/category/categoryname/page/3/
http://www.sample/year/month/day/page/3/
http://www.sample/year/month/page/3/

When your user is clicking one of those links, he/she will see an error message: “Sorry, no posts match that criteria,” while the page loads with all the surroundings (sidebar, header, footer). This is a result of a glitch in the generated .htaccess file. Delete the content of your .htaccess file and re-create it in order to fix it.

  1. Go to Manage > Files in the Control Panel,
  2. To edit the content, click the link to your .htaccess file
  3. In a text editor, paste the content to a text file. This is a precaution in case your .htaccess file has denials, handy htaccess tricks or manual entries for redirects. Delete all content from your .htaccess file and update the file.
  4. Go to Options > Permalinks in the Control Panel.
  5. To generate new rewrite rules for your permalinks, click the Update Permalink Structure button.
  6. Use a link that was broken before and test the results.
  7. Place manual htaccess entries after # END WordPress lines or before the # BEGIN WordPress. Add any manual htaccess entries back in your file.

You may also go through some similar actions like delete the .htaccess files from the server, create a new empty .htaccess file, change its permissions to 666, and then by clicking the Update Permalinks Structure button, generate a new set of htaccess rules in Options > Permalinks.

Go to WordPress support forums if that still doesn’t work.

Problems with Permalinks to pages

Your Permalink structure should be updated, if you have tried to navigate to a newly created Page and encountered an error. New rules must be generated and updated to .htaccess(WordPress 1.0 and later), or to the internal rewrites array (WordPress 2.0 or later) every time you add a new static Page to WordPress.

Problems with Permalinks to Ultimate Tag Warrior tag pages

If you are using the UltimateTagWarrior plugin on WordPress 2.X and get 404 errors on local tag URLs, it is because the internal rewrites generated by WordPress were invoked before UTW’s rewrite rules and are being overly greedy. This usually takes place when you use custom permalink structure (like /%postname%/). Hack the UTW plugin to place the UTW rewrites at the top of the internal rewrites array or switch your Permalink structure to “Date and name based” in order to fix this.

No pages are returned while Permalinks work correctly

There is a bug that causes mod_rewrite to fail when used with some versions of Apache 2.x in some versions of PHP 4.4.x and 5.x.

Tricks and Tips

How to Avoid an archive link interpretation

Pay attention that links generated like %year%%monthnum%%day% will however be interpreted as the archive of all posts for that day, even if one might never make more than one posting a day. To target an individual post, you need to use at least %year%%monthnum%%day%%hour% format.

Permalink Structure Checking

Paste the following code to check if the blog has a permalink structure:

<?php if ( get_option(‘permalink_structure’) ) { echo ‘permalinks enabled’; } ?>

Help

If these pieces of advice do not work, search for your problem in the Support Forum, Codex or Troubleshooting.



Popular Posts

Like This Article? Subscribe to Our Monthly Newsletter!

Comments are closed.

NEW YEAR SALE
FOR ALL PRODUCTS
-30%
FOR PLUGINS & THEMES

USE THE NEW25
PROMO CODE

Get Discount Now!

This will close in 26 seconds