URL Rewriting: How To Rewrite URLs In WordPress

WordPress is one of the most used CMS, today on the Internet. With the increasing demand of the Internet, it becomes mandatory to explore WordPress to its depth. Here, in this post we will discuss regarding the WordPress URL rewriting.

What is URL rewriting?

When you want a particular format in the URL, to display a particular content, we use URL rewriting options. For example the search query for the WordPress looks like ‘?s=abc’. To make it look good, we can reform this query and can make it as /search/abc. This way your URL looks pretty.

URL rewriting can also be used for making SEO friendly URLS. For example the default WordPress URLs for the blog posts are ‘?p=ID’. This URL is not readable and at the same time is not SEO friendly. Thus, using URL rewriting functionality of WordPress, we can make it look like ‘yoursite.com/post-name’. Now, this is what we call as pretty URLs, which are easy to read and are SEO friendly as well.

URL rewriting can simply be done by using the .htaccess file and specifying the URL rewriting rules. Let’s explore how this can be done with WordPress.

URL rewriting with WordPress

WordPress provides a function ‘add_rewrite_rule’ to specify new URL rewrite rule. The syntax to use the function is as below:

  1. <?php add_rewrite_rule($regex, $redirect, $after); ?>

There are three arguments viz. $regex, $redirect, $after.

$regex: This is a regular expression to match against the requested URL.

$redirect: The original URL that has to be fetched.

$after: This can take two values ‘top’ or ‘bottom’. The top will consider this as superior to the WordPress’s original rules.

To give a perfect example, here we will modify the URL for the author’s page. Eventually this option is not directly available under ‘permalinks’ tab in WordPress admin panel. For URL  rewriting we will call the custom function on ‘init’ hook.

  1. add_action( 'init', 'add_author_rules' );
  2. function add_author_rules() { 
  3.     add_rewrite_rule(
  4.         "writer/([^/]+)/?",
  5.         "index.php?author_name=$matches[1]",
  6.         "top");
  7.  
  8.     add_rewrite_rule(
  9.   "writer/([^/]+)/page/?([0-9]{1,})/?",
  10.   "index.php?author_name=$matches[1]&paged=$matches[2]",
  11.   "top");
  12.  
  13.     add_rewrite_rule(
  14.   "writer/([^/]+)/(feed|rdf|rss|rss2|atom)/?",
  15.   "index.php?author_name=$matches[1]&feed=$matches[2]",
  16.   "top");
  17.  
  18.     add_rewrite_rule(
  19.   "writer/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?",
  20.   "index.php?author_name=$matches[1]&feed=$matches[2]",
  21.   "top");
  22. }

This will rewrite the author’s page URL from ‘?author_name=name’ to ‘/writer/name’. This will make it look good and will also be URL friendly.

The other method that can be implemented is by changing the default rewriting rules. WordPress provides a filter called ‘rewrite_rules_array‘. Using this array, we can form the new rules and can merge the new rules with the original rules. Here for an example we have created a new set of rules in the function ‘new_rewrite_rules‘. This function returns a combination of the original set of rules along with the new rules that we developed.

  1. function new_rewrite_rules($rules) {
  2.  
  3. 	$newrules = array();
  4. 	$newrules["^ci\/1/?$"]   =  'index.php?page_id='.new_get_id_by_slug('/ci1');
  5.  
  6. 	$rules = $newrules + $rules; 
  7.  
  8. 	return $rules;
  9.  
  10. }
  11.  
  12. add_filter('rewrite_rules_array', 'new_rewrite_rules' , 1 , 1);
  13.  
  14. function new_get_id_by_slug($page_slug) {
  15. 	$page = get_page_by_path($page_slug);
  16. 	if ($page) {
  17. 		return $page->ID;
  18. 	} else {
  19. 		return null;
  20. 	}
  21. }

Here, in the above code, we write the rules for rewriting as below:

If someone enters the URL as YourDomain.com/ci1, it will render our original page which has the path as YourDomain.com/ci/1.

This way, with these two methods you can add new rewrite rules to WordPress!