Skip to content

Latest commit

 

History

History
57 lines (38 loc) · 2.52 KB

rewrite.md

File metadata and controls

57 lines (38 loc) · 2.52 KB

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.

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.

[alert type="warning"]Remember to flush rewrite cache after creating or removing rules. You can do that with wp assely:clear rewrites command or manually resave "Permalinks" settings in admin panel.[/alert]

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

Rewrite::rule('favourite/movies/top10');

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:

Rewrite::rule('favourite/movies/{from}/{to}');

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

Rewrite::rule('favourite/movies/{from}/{to}')->where([
    'from' => '([0-9]{4})',
    'to' => '([0-9]{4})'
]);

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.

[alert type="info"]Descriptions of all available places you can find in the Codex[/alert]

Rewrite::endpoint('json')->to(EP_PERMALINK);