The WordPress Rewrite API allows developers to programmatically specify new, custom rewrite rules. However, it is really unclear and unreadable. The Rewrite component closes all of this messy code into one easy to follow API.

Basics of Rewriting

All application custom rewrite rules should be defined inside app\Http\rewrites.php file. This file is included on application bootstrap by the App\Providers\HttpServiceProvider class.

Creating Rewrite Rule

The most basic custom rewrite rule is a static URI. You can define it with rule method and desired path string as argument:


Creating Rewrite Rule with Parameter

Often you will need to define dynamical segments of the URI. For example, to display a list of your favorite movies from the specific date range. You may do so by defining a rule with parameters:


Additionally, you can constrain the format of parameters with the where method. It takes an array of parameters names and regular expressions.

    'from' => '([0-9]{4})',
    'to' => '([0-9]{4})'

Creating Endpoint

Endpoints allow you to create a group of extra rewrite rules which will be applied to a group of all already created rules within the specifed place (like permalinks or pages).

Let’s assume you want to create links to JSON representation of movies. To do that, call endpoint method with json endpoint name. You also need to specify endpoint place with to method.