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:
<?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.
add_action( 'init', 'add_author_rules' );
function add_author_rules() {
add_rewrite_rule(
"writer/([^/]+)/?",
"index.php?author_name=$matches[1]",
"top");
add_rewrite_rule(
"writer/([^/]+)/page/?([0-9]{1,})/?",
"index.php?author_name=$matches[1]&paged=$matches[2]",
"top");
add_rewrite_rule(
"writer/([^/]+)/(feed|rdf|rss|rss2|atom)/?",
"index.php?author_name=$matches[1]&feed=$matches[2]",
"top");
add_rewrite_rule(
"writer/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?",
"index.php?author_name=$matches[1]&feed=$matches[2]",
"top");
}
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.
function new_rewrite_rules($rules) {
$newrules = array();
$newrules["^ci\/1/?$"] = 'index.php?page_id='.new_get_id_by_slug('/ci1');
$rules = $newrules + $rules;
return $rules;
}
add_filter('rewrite_rules_array', 'new_rewrite_rules' , 1 , 1);
function new_get_id_by_slug($page_slug) {
$page = get_page_by_path($page_slug);
if ($page) {
return $page->ID;
} else {
return null;
}
}
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!