In this lesson, you'll learn about a concept called Permalix and how they are used to rewrite dynamic URLs on a WordPress site.
In the web service lesson, you learn that most PHP-based applications will have a directory index file.
This is the file that will be executed when a user browses to the URL of the site in question.
With a site powered by WordPress, however, it's possible to have multiple different types of content rendered, like posts, pages or products, via the same directory index file.
The key feature behind how this works is called Permalix.
Before we dive into Permalix, it's useful to first understand a concept known as a query string.
Let's take a look at an example on a test WordPress installation.
For this post, the URL is wordpress.test and the query string is question mark P equals 1.
The query string is a way to pass data to the web server.
Here, the query string is passing the value of 1 to the variable P.
In PHP, it's possible to access the value of the variable P using the $get super global.
The PHP code can then use this to perform some sort of data lookup.
For example, to retrieve a post from the database with an ID of 1.
Permalinks, also known as clean URLs, are a way to make dynamic URLs more human readable.
Instead of using a query string, Permalinks use a URL structure that is based on the content being rendered.
Here is the same example as before, but this time using a Permalink.
In this example, the full URL is wordpress.test/1.
There is no query string and the URL is much more human readable.
But how does the web server know what content to serve?
Based on the expected URL structure, the web server can be configured to perform URL mapping,
which uses a web server feature called URL rewriting.
The web server can be configured to expect a certain URL structure
and then pass that data through to the web application,
which handles fetching the relevant information based on the data it receives.
In WordPress, this feature is called Permalinks and allows you to configure the URL structure of your WordPress site.
You can find this feature in the WordPress dashboard under Settings, Permalinks.
The default Permalinks structure is plain, meaning no Permalinks are in use and plain query strings will be used.
The other options allow you to set your desired Permalinks structure from a list of common options,
or define your own custom structure.
When you set any one of these options other than plain,
the server will be configured to expect a clean URL based on the structure that you have defined.
At the same time, WordPress will store the selected structure in the database.
When a request is made to the site using the matching structure,
WordPress will use these two pieces of data to map the URL structure to the information that needs to be displayed,
fetch that information, and display it on the page.
On an Apache web server, this is typically handled in the htaccess file.
For example, if you set your Permalinks structure to any of the options other than plain,
the following code will be added to your htaccess file.
This code checks if the Apache web server has the rewrite module enabled,
and then sets up the rewrite rules to expect a specific URL structure.
On nginx web servers, this is typically handled in the server block configuration file.
Because nginx does not support the use of an htaccess file like configuration on a WordPress level,
the configuration is typically added by default to the server block in a location directive.
Whenever a Permalinks structure is set, if you add links to internal content like posts or pages,
WordPress will automatically generate the correct URL based on the Permalinks structure that you have set.