Wordpress custom Querystrings & Pretty URL - How?

I have a great (so far) wordpress installation for a new website. Cute URLs work as expected.

I have one dynamic page that loads content depending on the request:

/dynamic/?loc=england&code=uk 

I want to make this URL “pretty,” but every time I change .htaccess, no changes have been made. I tried everything and looked for everything else - the last resort. If I could get the following to work, I would be happy.

I need to make the url look like.

/dynamic/location/england/code/uk/

Adding this to any .htaccess crashes the entire website.

RewriteRule /dynamic/(.*)/(.*)/(.*)/(.*)/$ /dynamic?$1=$2&$3=$4

What am I missing.

Thanks in advance

N

+3
source share
1 answer

.htaccess. WordPress , , .

WordPress , - , .

, , . RegEx, WordPress , (i.e dynamic/location/(.*)/code/(.*) = > /dynamic?$loc=$1&code=$2). script .

URL WordPress. , , ( note: untested!!!):

<?php
add_action('init', 'add_my_rewrite');

function add_my_rewrite() {
    global $wp_rewrite;
    $wp_rewrite->add_rule('location/([^/]+)/code/([^/]+)','index.php?loc=$matches[1]&code=$matches[2]','top');
    $wp_rewrite->flush_rules(false);  // This should really be done in a plugin activation
}

rewrite .htaccess .

+11

Source: https://habr.com/ru/post/1783470/


All Articles