If you’ve ever looked at a WordPress URL and wondered, “How does WordPress understand this?” — you’re already touching the Rewrite API, whether you know it or not.
Pretty URLs like:
/blog/my-first-post//course/wordpress-basics/lesson-3//product/t-shirt/
don’t magically work. Behind the scenes, WordPress translates those URLs into database queries. That translation system is called the Rewrite API.
Here’s the thing: most developers use WordPress for years without fully understanding how rewriting works. Then one day they need a custom URL structure, and everything breaks.
Let’s break it down properly, in plain language.
What the Rewrite API Actually Does
At its core, the Rewrite API does one job:
It converts human-friendly URLs into query variables that WordPress understands.
Your browser requests this:
/course/wordpress-basics/
WordPress internally turns it into something like:
index.php?post_type=course&name=wordpress-basics
That’s it. That’s the whole purpose.
Everything else is just tools and rules to make that conversion happen.
Why WordPress Needs Rewrite Rules
WordPress is built on PHP and MySQL. PHP doesn’t understand URLs like /about-us/ by default. It understands query strings like:
index.php?page_id=12
Rewrite rules act like a map:
- If the URL looks like this → load this content
- If the URL matches that pattern → run this query
Without rewrite rules:
- Permalinks wouldn’t work
- Custom post type URLs wouldn’t work
- Pretty WooCommerce URLs wouldn’t exist
Where Rewrite Rules Live
Rewrite rules exist in three places, and this part confuses many people.
1. The .htaccess File (Server Level)
This file tells Apache:
“Send all requests to index.php.”
WordPress doesn’t store individual rewrite rules here. It only routes traffic to PHP.
So .htaccess is important, but it’s not where WordPress logic lives.
2. WordPress Rewrite Rules (PHP Level)
This is where the real logic happens.
WordPress stores rewrite rules in the database under:
wp_options → rewrite_rules
These rules are generated using:
- Post types
- Taxonomies
- Permalink settings
- Custom rewrite rules added by developers
WordPress checks these rules top to bottom until it finds a match.
3. Query Variables
Once a rule matches, WordPress sets query variables like:
post_typenamepagenametaxonomyterm
These variables are then used by WP_Query to fetch data.
How a Request Flows Through the Rewrite System
Let’s walk through a real request.
Step 1: User Requests a URL
/course/wordpress-basics/
Step 2: Server Sends It to WordPress
Apache or Nginx sends the request to index.php.
Step 3: WordPress Checks Rewrite Rules
WordPress loops through rewrite rules and looks for a matching pattern.
Example rule:
course/([^/]+)/?
Step 4: Rule Matches and Sets Query Vars
WordPress sets:
post_type = course
name = wordpress-basics
Step 5: WP_Query Fetches the Content
Now WordPress knows exactly what content to load.
Custom Post Types and Rewrite Rules
When you register a custom post type, WordPress automatically creates rewrite rules for you.
Example:
register_post_type( 'course', [
'public' => true,
'rewrite' => [ 'slug' => 'course' ],
] );
This tells WordPress:
- Use
/course/as the base slug - Generate rewrite rules automatically
So /course/wordpress-basics/ just works.
This is why most developers never touch the Rewrite API directly. WordPress handles it for them.
When You Actually Need the Rewrite API
You need to work directly with rewrite rules when:
- You want non-standard URLs
- You want URLs that don’t map directly to posts
- You’re building plugins, not just sites
- You want URLs like
/dashboard/settings/ - You want custom filtering URLs like
/jobs/location/dhaka/
In these cases, default post type rewriting isn’t enough.
Adding a Custom Rewrite Rule
Here’s a basic example.
add_action( 'init', function () {
add_rewrite_rule(
'^dashboard/settings/?$',
'index.php?pagename=dashboard§ion=settings',
'top'
);
} );
What this means:
- If the URL matches
/dashboard/settings/ - Load the page
dashboard - Set a custom query variable
section=settings
Registering Custom Query Variables
WordPress ignores unknown query variables unless you register them.
add_filter( 'query_vars', function ( $vars ) {
$vars[] = 'section';
return $vars;
} );
Now WordPress allows section to exist.
Using the Query Variable
Inside your template:
$section = get_query_var( 'section' );
if ( $section === 'settings' ) {
// Load settings section
}
This is how plugins like WooCommerce, LMS plugins, and membership plugins build complex URLs without extra pages.
Flush Rewrite Rules (The Right Way)
Rewrite rules are cached.
That’s why things break when:
- You add a new rule
- You register a new post type
- URLs don’t work until you save permalinks
The Safe Way
Flush rules only once, on activation.
register_activation_hook( __FILE__, function () {
flush_rewrite_rules();
} );
register_deactivation_hook( __FILE__, function () {
flush_rewrite_rules();
} );
The Wrong Way
Never do this:
flush_rewrite_rules();
inside init or page load. That kills performance.
Rewrite Rule Priority: Top vs Bottom
Rewrite rules are checked in order.
topmeans higher prioritybottommeans lower priority
If your rule conflicts with others, use top.
Example:
add_rewrite_rule( '^course/([^/]+)/lesson/([^/]+)/?', 'index.php?...', 'top' );
Be careful. Too many top-level rules can cause conflicts.
Common Rewrite API Mistakes
1. Forgetting to Flush Rules
New rules won’t work until flushed.
2. Not Registering Query Vars
Your rule matches, but WordPress ignores the data.
3. Conflicting Slugs
Using the same slug for:
- Page
- Post type
- Taxonomy
This causes unpredictable behavior.
4. Overusing Rewrite Rules
Sometimes a page template or query parameter is enough.
Debugging Rewrite Issues
Useful tools:
- Enable
WP_DEBUG - Use Query Monitor plugin
- Check
rewrite_rulesoption in database - Temporarily switch to plain permalinks and back
To inspect rewrite rules:
global $wp_rewrite;
print_r( $wp_rewrite->rules );
Only do this in development.
Rewrite API vs REST API
Important distinction:
- Rewrite API controls URLs
- REST API controls data access
They solve different problems.
You can build:
- Pretty URLs with Rewrite API
- JSON endpoints with REST API
Many advanced plugins use both together.
Real-World Examples
- WooCommerce product URLs
- Tutor LMS course and lesson URLs
- Membership dashboard URLs
- Custom filtering pages
- SEO-friendly archive pages
Once you understand rewrite rules, you start seeing WordPress differently. URLs stop being magic. They become logic.
When Not to Use Rewrite Rules
Don’t use rewrite rules if:
- A normal page works
- A query parameter is enough
- You don’t need SEO-friendly URLs
- You’re just experimenting
Rewrite rules are powerful, but with power comes complexity.
Final Thoughts
The WordPress Rewrite API isn’t scary. It’s just misunderstood.
At a basic level:
- URL comes in
- Rule matches
- Query vars are set
- WordPress loads content
That’s all.
Once you understand that flow, you can build:
- Cleaner URLs
- Better user experiences
- More professional plugins
And most importantly, you stop guessing.
If you want next:
- Rewrite API + WooCommerce examples
- Rewrite API for LMS platforms
- Visual diagrams for rewrite flow
- Common rewrite patterns you can reuse
Just tell me what you want to build.