Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    You do not use rewrite rules to manage custom query vars like “id”. Rewrite rules are for taking elements within permalinks like “/project/cookies/” and translating to URL query vars like “id=1”. You’re trying to go the wrong direction!

    I’d advise against common names like “id”, there’s too much chance of a conflict developing with your theme or other plugins using the same name. Something like “ubsp-id” is much better. You could prefix all of your custom variables and custom functions with the same “ubsp” or whatever characters you like to help avoid conflicts.

    Anyway, I think what you might actually be wanting to do is to have WP output an URL like blubb.enif.uberspace.de/wordpress/project/cookies/ and rewrite it to blubb.enif.uberspace.de/wordpress/project/?id=1 when requested. That would involve some sort of look up to convert “cookies” to “1”. Rewrite rules cannot do that. They can only use the data at hand. You can rewrite the cookies permalink to blubb.enif.uberspace.de/wordpress/project/?id=cookies, then have other code, a callback for “pre_get_posts” for example, take the query var “id” value of “cookies” and look up the true ID of “1” somehow, then use that to construct a query that returns posts related to cookies.

    There’s two sides to permalinks. Outputting them on pages as links, then interpreting the request, perhaps rewriting certain elements as query vars. With all this in mind, please rephrase what you are trying to do within this context and we can figure out how to accomplish it.

    Thread Starter Insomnia88

    (@insomnia88)

    Thanks for the feedback.

    Seems like I got something wrong. Still, it’s like I wrote. I want to change a “dynamic” url with custom parameters to be rewritten to something more readable.

    A more specific example. I want
    https://blubb.enif.uberspace.de/wordpress/project/?foo=bar

    to look like
    https://blubb.enif.uberspace.de/wordpress/project/bar/

    I tried a basic rewriterule in the htaccess file but didn’t get to work it there (maybe my rewrite rules were wrong).

    Moderator bcworkz

    (@bcworkz)

    Yeah, rewrite rules only work one way, reverse from what you want. So, you want incoming requests with query vars foo=bar rewritten to a permalink style? Not links being output to browser?

    If so, WP can make use of the query vars directly. If it leads to a post with a permalink like you desire, and your permalink settings are configured like that, WP will rewrite the address automatically. The only code you need is something to process foo query var to get the correct post. This could be done in the “pre_get_posts” action. For foo to be visible as a query var, it needs to be white listed in the “query_vars” filter.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘custom rewrite rules not working?’ is closed to new replies.