Displaying Post with Custom URL with RewriteRule or add_rewrite_rule


I’m having what I believe is a common issue: I want to display a post with a slug with a custom URL, in my case, this is the format /resources/{state}/{city}/{location}/ which would match a post slug of resources-{state}-{city}-{location}. If it’s important, states, cities and locations can contain dashes as well.

None of the following have worked (and note that I save in Settings->Permalinks before every attempts):

In .htaccess before # BEGIN WordPress: RewriteRule ^resources/([^/]*)/([^/]*)/([^/]*)/?$ /index.php?pagename=resources-%1-%2-%3 [L,NC]

In functions.php (with and without the initial carat in the first argument, as well as escaped and unescaped):

add_action( 'init', 'wpse_redirect' );
function wpse_redirect() {
        'top' );        

and with and without some solutions mentioned elsewhere stopping canonical redirection discussed here: https://www.kondorwithak.com/blog/stopping-wordpress-redirecting-matched-slugs/

In various combinations, I sometimes see the homepage at my custom URL, I see a 404 at my custom URL, and of course, am redirected to the WordPress permalink so it seems as though I’m close.

Does anyone have any words of wisdom for me?

Thanks in advance.

Evan Rose 2 months 0 Answers 10 views 0

Leave an answer