Skip to content
Y
yTube.SITE v8.0.4
media / worlds / tools / internet oddities
YouTube / video information

How to Extend WordPress URLs with Extra Path Information Using Custom Rewrite Rules

Video information, playback and saved actions.

How to Extend WordPress URLs with Extra Path Information Using Custom Rewrite Rules

Description: Learn how to convert WordPress page URL query parameters to clean, user-friendly URLs with extra path segments by adding custom rewrite rules and query variables.
---
This video is based on the question https://stackoverflow.com/q/78923609/ asked by the user 'Alberto' ( https://stackoverflow.com/u/605371/ ) and on the answer https://stackoverflow.com/a/78925092/ provided by the user 'Alberto' ( https://stackoverflow.com/u/605371/ ) at 'Stack Overflow' website. Thanks to these great users and Stackexchange community for their contributions.

Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: Extending wordpress adding extra path information to a page URL

Also, Content (except music) licensed under CC BY-SA https://meta.stackexchange.com/help/licensing
The original Question post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/by-sa/4.0/ ) license, and the original Answer post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/by-sa/4.0/ ) license.

If anything seems off to you, please feel free to drop me a comment under this video.
---
Introduction

When using WordPress, sometimes you want to make URLs cleaner and more user-friendly by replacing query parameters with additional path information. For example, instead of http://example.com/pagename?query=foo, you want http://example.com/pagename/foo. WordPress needs to understand this structure internally to serve the correct content.

This blog explains how to add custom rewrite rules and register new query variables to extend URLs with extra path segments in a WordPress page.



The Problem

WordPress natively uses query variables (like ?query=foo) to pass data to pages. But it doesn't automatically treat a URL with an extra path segment (like /pagename/foo) as the same page with additional data.

To achieve example.com/pagename/foo, WordPress must be told:

How to interpret the extra path foo.

That foo is a query variable tied to pagename.



The Solution

1. Add a Custom Rewrite Rule

Use add_rewrite_rule() in your theme's functions.php or a custom plugin to map the new URL structure:

[[See Video to Reveal this Text or Code Snippet]]

Explanation:

^pagename/([^/]+ )/?$ matches URLs like pagename/foo.

Captured part (foo) is passed as word query variable.

Internally, WordPress serves the pagename page.

2. Register the Query Variable

By default, WordPress ignores unknown query variables. Declare your custom variable (word) so it’s accessible:

[[See Video to Reveal this Text or Code Snippet]]

3. Use the New Variable in Your Template

In your page template or plugin, access the additional path data with:

[[See Video to Reveal this Text or Code Snippet]]

Note: Use rawurldecode to properly handle UTF-8 or encoded characters.

4. Flush Rewrite Rules

After adding or modifying rewrite rules, flush WordPress’s permalink cache manually by visiting Settings → Permalinks in the WordPress dashboard and clicking Save Changes (no need to change settings). This step updates rewrite rules so your URL works immediately.



Summary

To replace query parameters with clean URL path segments in WordPress:

Add a custom rewrite rule to map URLs.

Register the query variable to make it accessible.

Use get_query_var() to retrieve the data.

Flush rewrite rules by saving permalinks.

This approach ensures your URLs remain clean, understandable, and SEO-friendly without compromising WordPress’s routing system. Close description
      
Videos related to "How to Extend WordPress URLs with Extra Path Information Using Custom Rewrite Rules"

Related videos are temporarily unavailable.

Try again, or come back later.