Search

How to Create Custom WordPress Rewrite Rules for Pretty Permalinks

Listen to this article

Rewrite Rules WordPressDo you know how WordPress displays content a web page? What actually happens behind the scenes, when a URL is entered?

For example, consider the URL http://example.com/?p=123. It is easy to infer that the content for the post with ID 123 will be displayed on the page. WordPress also understands this based on the query variables passed (p in this case). In fact, the URL is actually read as http://example.com/index.php?p=123. Since index.php would be basically used to reach the required post.

The same logic can be applied to display posts tagged with WordPress. Your url would be http://example.com/?tag=wordpress. To display the feed for the link, you would have to add feed variable into the query http://example.com/?tag=wordpress&feed=rss. As you must have noticed, the more query variables you add, the more “un-pretty”, or rather illegible, the link structure becomes.

 

Introducing Pretty Permalinks

To make the permalink structure more human friendly, it is better to use pretty permalinks. Pretty permalinks make a URL structure appear more readable, which makes links easier to remember. For example, http://example.com/?tag=wordpress&feed=rss would be http://example.com/tag/wordpress/feed/rss/

But for WordPress to resolve such links, there has to be a way to interpret the query variables from the specified link. For this, we make use of Rewrite rules. Rewrite rules are regular expressions which basically help you go from

Pretty Permalinks: Go from User Friendly to WP friendly

 

What are Rewrite Rules?

Rewrite rules are added in your .htaccess files, to resolve pretty permalinks. Without these rules, you’d end up getting a 404 Page Not found error. If you look at your .htaccess file, you’ll see the following:

[pre]# 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[/pre]

So how do these rules actually work.

[space]

How Does Rewrite Work?

These rules, check the URL entered. First off, it checks if there is a directory specified, and an eventual filename. If so, it simply displays the file.

For example, http://example.com/wp-content/uploads/2014/07/filename.jpg would display filename.jpg present in ‘uploads’->‘2014’->‘07’ directory. And when it is not a file path, it converts the URL into query parameters for index.php.

For taxonomies, users and post types defined by WordPress, WordPress adds it’s own rewrite rules. To view the rewrite rules added, you can make use of the Rewrite Rules Inspector.

Default WordPress Rewrite Rules for Tags

Let’s take a look at the expression tag/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$ which is eventually converted to index.php?tag=$matches[1]&feed=$matches[2]

  1. This conversion is handled by the parse_request method, present in class-wp.php
  2. tag/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$ implies tag, followed some-tag-name-string, followed by feed, followed by feed value (feed or rss or rss2 or atom)
  3. This will be converted to index.php?tag=$matches[1]&feed=$matches[2], where tag will be assigned the value some-tag-name and feed will be assigned one of the feed values.
  4. The matches array contains the matched values, indexed from 1.

 

How to Add Your Own Rewrite Rules

Consider that you have to create a Overview page for every author on the site. The page would be something like a profile page, which would display author bio. If you think about it, the access to this page would be some link like http://example.com/?author=12&overview=1. But in case you wanted to support pretty permalinks, you’ll have to define your own rewrite rules, to convert the pretty permalink into a WP legible link.

This can be achieved by the following steps:

  1. Create a Rewrite Rule
  2. Add a Query Variable
  3. Interpret the URL

 

Create Rewrite Rule

The add_rewrite_rule function can be used to define a rewrite rule. You need to convert the permalink, into a regular expression. Hence, for your overview page link, which would be something like this http://example.com/author/author-name/overview/, the regular expression would be author/([^/]+)/overview/?$

 

[pre]function wdm_add_rewrite_rules()
{
add_rewrite_rule( ‘^author/([^/]+)/overview/?$’,
‘index.php?author_name=$matches[1]&wdm_overview=1,
‘top’);
}
add_action(‘init’,’wdm_add_rewrite_rules’);[/pre]

The parameter ‘top’ decides where the rule will be placed. Either at the beginning of the rewrite list or at the end (bottom). The rules are matched according to their order.
[space]

Add a Query Variable

If you notice, we have used a query variable wdm_overview. This is to help us understand that the overview page has to be displayed. But for WordPress to recognize the new query variable, we have to add it to the WP_Query’s query_vars array.
[pre]function wdm_add_queryvars( $query_vars ) {
$query_vars[] = ‘wdm_overview’;
return $query_vars;
}
add_filter( ‘query_vars’, ‘wdm_add_queryvars’ );
[/pre]

[space]

Interpret the URL

The final step of course is to interpret the URL and display the content. For this we have to add a handler, which will check if wdm_overview parameter is set. We will have to create an overview template page, and use it to display the content.

[pre]function wdm_is_overview_page()
{
global $wp_query;

if(isset($wp_query->query_vars[‘wdm_overview’])&&($wp_query->query_vars[‘wdm_overview’] == 1) )
{
// set template author-overview.php
}
}
[/pre]

[space]

Do note, if you do not see the new rewrite rules taking effect, you might have to flush the rewrite rules, by calling the flush_rewrite_rules() function. Flushing the rewrite rules, updates the rules in the database. Your changes might not be recognized, until you do so. But this is an expensive operation. So, it is best to call this rule when a plugin is activated and deactivated.

 

Pooja Mistry

Pooja Mistry

One Response

  1. Hey Pooja, Thanks for sharing this tutorial as i have seen the code from https://www.wpblog.com/wordpress-url-rewrite-for-custom-page-template/ and i am implementing the code properly but still having errors and i don’t know whether i am doing mistake.

    function prefix_news_rewrite_rule() {
    add_rewrite_rule( ‘news/([^/]+)/photos’, ‘index.php?news=$matches[1]&photos=yes’, ‘top’ );
    add_rewrite_rule( ‘movie/([^/]+)/videos’, ‘index.php?news=$matches[1]&videos=yes’, ‘top’ );
    }

    add_action( ‘init’, ‘prefx_news_rewrite_rule’ );

Leave a Reply

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

Get The Latest Updates

Subscribe to our Newsletter

A key to unlock the world of open-source. We promise not to spam your inbox.

Suggested Reads

Join our 55,000+ Subscribers

    The Wisdm Digest delivers all the latest news, and resources from the world of open-source businesses to your inbox.

    Suggested Reads