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.
All the blog post links will have URLs that follow the format that you have specified, but if you click any one of those links the Web server will return a 404 – File Not Found error. This is because WordPress relies on a URL rewriting capability within the server to rewrite requests that have “pretty permalinks” to an Index.php file
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.
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.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.
Creating a Rewrite Rule
Open the Web.config file that is located in the same directory where the WordPress files are installed, and paste the following XML section into the system.webServer element:
This rule will try to match any requested URL. If the URL does not correspond to a file or a folder on the file system, it will rewrite the URL to the Index.php file.
Testing the Rewrite Rule
After you save the rewrite rule to the Web.config file, open a Web browser and click any one of the permalinks in your WordPress blog. You should see the correct content returned by the Web server for each permalink.
In this walkthrough you learned how to use the URL Rewrite module to enable “pretty permalinks” in the WordPress blog engine.