Displaying Post with Custom URL with RewriteRule or add_rewrite_rule

Question

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() {
    add_rewrite_rule(
        'resources/([^/]*)/([^/]*)/([^/]*)/?$',
        'index.php?pagename=resources-$matches[1]-$matches[2]-$matches[3]',
        '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.

0
Evan Rose 8 months 2020-12-10T16:10:32-05:00 0 Answers 10 views 0

Leave an answer

Browse
Browse