{"id":300,"date":"2013-11-15T16:02:15","date_gmt":"2013-11-15T16:02:15","guid":{"rendered":"http:\/\/www.webtipblog.com\/?p=300"},"modified":"2013-11-15T16:05:27","modified_gmt":"2013-11-15T16:05:27","slug":"update-url-rewrite-rules-wordpress","status":"publish","type":"post","link":"https:\/\/www.webtipblog.com\/update-url-rewrite-rules-wordpress\/","title":{"rendered":"Update URL Rewrite Rules in WordPress"},"content":{"rendered":"
\nURL rewriting is the process of creating a URL that is friendly for users to look at, type, and share and is usually referred to as a pretty URL. URL rewriting allows you to take query parameters and add them to the URL instead of after the ? in the URL. WordPress already does this for the built in taxonomies such as going to a post or page. When you enable permalinks in WordPress and then go to a post url such as www.mysite.com\/my-favorite-post, the real page that gets loaded is www.mysite.com\/index.php?p=1. WordPress also uses rewrites for pagination. When you go to page 2 of a page such as www.mysite.com\/page\/2 the real url you are directed to is www.mysite.com\/index.php?paged=2. Custom rewrite rules can be used to add variables to the url without using query parameters, which can be very useful in plugin development.\n<\/p>\n
\nIt all starts in the .htaccess file that WordPress updates when you enable permalinks, note that it directs urls that can’t be found to index.php. So when you navigate to www.mysite.com\/page\/2, the page directory is not found and you are redirected to \/index.php. The .htaccess file WordPress creates looks something like this.<\/p>\n
# BEGIN WordPress\r\n<IfModule mod_rewrite.c>\r\nRewriteEngine On\r\nRewriteBase \/\r\nRewriteRule ^index\\.php$ - [L]\r\nRewriteCond %{REQUEST_FILENAME} !-f\r\nRewriteCond %{REQUEST_FILENAME} !-d\r\nRewriteRule . \/index.php [L]\r\n<\/IfModule>\r\n\r\n# END WordPress<\/pre>\nThe index.php file then parses the URL that was called, in this example www.mysite.com\/page\/2 and runs it through a series of regexes to determine what the query variables are. WordPress has a rewrite rule regex for pagination that looks like this:<\/p>\n
page\/?([0-9]{1,})\/?$<\/pre>\nSo any URL that matches that regex is converted to index.php?&paged=$matches[1]. <\/p>\n
Rewrite Rules<\/h3>\n
\nAdding a rewrite rule can be done with the add_rewrite_rule()<\/a> function. The function takes three parameters: the first is the regex that will match the rule, the second is the rewritten URL, the third is whether to place the rule at the top or the bottom of the list of rewrite rules. To add a rewrite rule that will recognize “\/pageOrPostName\/MyNewVar\/anyValue” at the end of a url, we could add the following rule.<\/p>\n
add_rewrite_rule(\r\n '^([^\/]*)\/MyNewVar\/([^\/]*)\/?',\r\n 'index.php?pagename=$matches[1]&newVariableName=$matches[2]',\r\n 'top'\r\n);<\/pre>\nOne final note here is that for the rewrite rule to actually be added and used by WordPress, the flush_rewrite_rules()<\/a> function needs to be called.<\/p>\n
flush_rewrite_rules();<\/pre>\n<\/p>\nRewrite Tags<\/h3>\n