• Good evening, guys!

    I haven’t done URL rewriting and such for a long time, but I need it to be done soon.
    I’ve tried some things out like add_rewrite_rule, but for unknown reasons it just won’t work as I want it to operate.

    What I need to rewrite is for example: https://www.example.com/category1
    It should rewrite the following URL: https://www.example.com/shop?filters=marken%5B20%5D

    So my unsuccessful code was:
    add_rewrite_rule(‘category1’, ‘shop?filters=marken[20]’, ‘top’);

    It just leads me to the index page instead of the shop page.
    I think the bracket characters are used in the htaccess-language, but I don’t know how to avoid issues with them.

    I hope somebody can help me out of this misery!
    Best regards! ??

Viewing 4 replies - 1 through 4 (of 4 total)
  • Im not saying that this will solve it, but normally when you escape such characters you use a backslash \

    Eg:

    add_rewrite_rule('category1', 'shop?filters=marken\[20\]', 'top');

    Thread Starter MrSteed

    (@mrsteed)

    Hey,

    thanks for your answer!

    I’ve already tried that, but for some reason the whole method doesn’t work.

    Now I’ve installed a plugin for mod rewrites, but it also doesn’t work with [] brackets.

    Attempting to escape the brackets adds another backslash after saving the changes, really weird… so from shop?filters=marken\[20\] it turns into shop?filters=marken\\[20\\] and so on.

    Is there anyone who experienced a problem like this?
    Or any other solutions?

    I NEED to rewrite the URLs because of SEO and WordPress doesn’t allow brackets in menue links. So I just can’t put the link like shop?filters=marken[20] into the menue, the brackets simply get replaced by wordpress.

    I dont know what I was thinking when I posted that yesterday. Since its in a GET call you need to urlencode them.

    add_rewrite_rule('category1', 'shop?filters='.urlencode('marken[20]'), 'top');

    Or hardcode it to:
    %5B20%5D

    [ = %5B
    ] = %5D

    You can use the following site https://www.url-encode-decode.com/

    Edit:
    You do this in an init action right? And have you saved the permalinksettings again?

    Thread Starter MrSteed

    (@mrsteed)

    Hey,

    maybe there’s something wrong with my wordpress setup at all…

    I added the following code to my functions.php:

    function cool_rewrite()
    {
    	add_rewrite_rule('c1', 'shop?filters='.urlencode('marken[20]'), 'top');
    }
    add_action('init', 'cool_rewrite');

    Now nothing happens, I simply get the 404 page opening /c1 .

    With the rewrite plugin I tried to rewrite the cat1 page with the shop page, that DOES work! At least one thing working.. ??
    https://prntscr.com/7afeb2

    But if I try to rewrite the license.txt (just for testing), I simply get the start page.
    https://prntscr.com/7aff21

    Is there anything wrong in the specific htaccess syntax or something?

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘URL rewrite with special characters’ is closed to new replies.