I had to create 3 page templates to display some content retrieved from different webservices outside of Wordpress.

The templates are:


I've also created 3 new pages and associated the templates.

collections --> page-collections.php
collection --> page-collection.php
product detail --> page-productDetail.php

Since the content comes from external webservices I don't have an url to access a specific collection / product. So in order to achieve 'pretty' urls I'm trying to rewrite an url from:


Where exampleName is the name of a collection which I use to retrieve the content from the ws.



I've placed the following rule before the # BEGIN WordPress directive:

<IfModule mod_rewrite.c>

# Turn mod_rewrite on
RewriteEngine On
RewriteRule ^(collections)/(.+)$ /collections/collection/?collectionName=$2 [L]


But instead of the expected page I get a 404 error.

If I replace [L] with [R=301,L] the rewrite works but it forces a 301 redirect which is not what I want. I just need the rewrite to happen.

Do you have any hint on why this happens?

  • 1
    What's the reason for doing this? Normally people are trying to do the opposite, what is name? Note that name is a reserved keyword as it's used in WP_Query and rewrite rules as a query argument. If you are trying to use it for logic in a page template then that will never work, and you can't use it. name is used by WordPress to request posts with a specific slug
    – Tom J Nowell
    Jul 23, 2021 at 13:52
  • 1
    If it's actually collection can you change your examples? By trying to make it generic you've hidden your problem and introduced an unrelated bug, until you fix this your question is either extremely confusing, or you'll get answers trying to fix ?name= which are useless to you ( you can't rely on people reading comments either )
    – Tom J Nowell
    Jul 23, 2021 at 14:14
  • 1
    @TomJNowell yes sorry, will edit with more informations and updated example
    – LS_
    Jul 23, 2021 at 14:15
  • 2
    @TomJNowell I've added a bit of context and updated the examples, let me know if something is still unclear. Regarding the last questions, I wanted this to be just a rewrite, so if a user access /test/test-detail/abc he keeps the same url but the actual loaded page becomes /test/test-detail?collection=abc. Also sorry if i'm using some wrong terminology but i'm a newbie when it comes to htaccess
    – LS_
    Jul 23, 2021 at 14:35
  • 1
    Thanks those edits are super useful!
    – Tom J Nowell
    Jul 23, 2021 at 14:37

1 Answer 1


You can add a rewrite rule to map to one of those pages with something like this:

add_action( 'init',  function() {
    add_rewrite_rule( 'myparamname/([a-z0-9-]+)[/]?$', 'index.php?myparamname=$matches[1]', 'top' );
} );

The first parameter of add_rewrite_rule is a regular expression to match a URL. The second parameter is an index.php non-pretty permalink URL, the URL parameters map directly into WP_Query arguments.

So if we take your first page as an example collections/collection/example:

add_action( 'init',  function() {
    add_rewrite_rule( 'collections/collection/([a-z0-9-]+)[/]?$', 'index.php?name=collection&collection_parameter=$matches[1]', 'top' );
} );

name=collection tells WordPress we want the page with the slug collection, and collection_parameter is how we're passing the final parts value.

We then need to allow the use of collection_parameter, and adjust the page template to use it instead of $_GET

To grab the value from your page template:

$collection = get_query_var( 'collection_parameter' );

To whitelist the parameter we need to add it to the whitelist:

 * Register custom query vars
 * @param array $vars The array of available query variables
 * @link https://codex.wordpress.org/Plugin_API/Filter_Reference/query_vars
function myplugin_register_query_vars( $vars ) {
    $vars[] = 'collection_parameter';
    return $vars;
add_filter( 'query_vars', 'myplugin_register_query_vars' );

Don't forget to flush rewrite rules, and you can extend this for the other two pages.

  • 1
    works perfectly, thank you very much!!
    – LS_
    Jul 23, 2021 at 14:56
  • 1
    Glad to be of help, you might be interested in looking up virtual pages, which build on this to eliminate the need for a page and a page template, do a search on the site and you'll find a few questions about this that should be informative
    – Tom J Nowell
    Jul 23, 2021 at 15:08
  • I'll for sure look that up in the coming days, might come in handy. Again, thank you for all the informations, really helpful!
    – LS_
    Jul 23, 2021 at 15:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.