How To Rewrite URLs In WordPress
In this tutorial we are going to look at how you can use the built in WordPress rewriting API to create your own unique links for your website. WordPress will already use this for it's own functionality when you select a new permalink structure.
What Is Rewriting?
URL rewriting is when you change what content is displayed by the server, normally when you type in a URL the web server will simply search for any files in that location. But with rewriting you can still show the same URL but in the back-end change where the server will look for the content. Therefore we can have a URL of /website-1 but tell the server to display content that is located in /website-1/content/index.php
How WordPress Uses Rewrite For Custom URLs
When you go to Settings -> Permalinks, you will need to select how you want your URLs to appear on WordPress. You have many different options to choose from the default WordPress will use is ?p={post_id}, this is the value that is stored in the wp_posts table and what the rewrite API will write to, when it wants to display the post data.
When you select a new permalink structure WordPress will store these rules in the wp_options table so it will know how to search the post table for the correct post. This means that we can have URLs with just the post title in them and WordPress will know that it needs to search on the post_name column to find the correct post to display.
![Permalinks](https://www.paulund.co.uk/app/uploads/2011/09/old_permalinks.png)
Changing the permalink can change your URLs from
http://www.paulund.co.uk/?p=1
To
http://www.paulund.co.uk/post-title
There are two main benefits of changing the URL structure. One is for SEO reasons, having keywords in the URL give a boost in the rankings as it's a indicator to what the page is about. Another benefit is that it's more user friendly clicking on a link with keywords in the URL.
How Does WordPress Do This?
When you change the permalink structure in WordPress it will make a change the htaccess file which will send all traffic on the website to the index.php file. If you open your htaccess file you will see something similar to this.
# BEGIN WordPress
<IfModulemod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
This will tell the website to send everything to the index.php file, this file will do some checks to work out where the file is that you are trying to access. First it will search to see if the file you want exists in the file structure, if it does it will simply take you to this file. If the file doesn't exist it will need to do some checks to see if it can find the content in the database and will use the rules you setup in the permalink page and the current URL to work it out.
This works fine if you want to use the the default settings with your website but if you want to create your own custom rules then you need to use the built in add_rewrite_rules() function to create a new reg ex pattern of how to rewrite the pages.
Default Rewrite Rules
Below is a list of all the default rewrite rules that you get with WordPress. The first is for the category pages this will allow you to have a URL for the categories of /category/films, there is a /page/ for the pagination on the category pages. The rewrite rule will then rewrite the request to go to index.php?category_name=films so that WordPress displays all the posts that are listed in the category of films.
There is also a tag URL that will work the sameway as the category but will search for all posts that have the tag assigned to them
Next is a date URL that will allow you to search for posts on a specific date by using the URL /2014/03/01 this will search for all posts that were published on the 1st of March 2014.
Finally the last rewrite rule will use the post name and from the URL and search for posts with this name.
category/(.+?)/page/?([0-9]{1,})/?$ => index.php?category_name=$matches[1]&paged=$matches[2]
category/(.+?)/?$ => index.php?category_name=$matches[1]
tag/([^/]+)/?$ => index.php?tag=$matches[1]
page/?([0-9]{1,})/?$ => index.php?&paged=$matches[1]
([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/?$ => index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]
(.+?)(/[0-9]+)?/?$ => index.php?pagename=$matches[1]&page=$matches[2]
Comments
Post a Comment