htaccess redirects with special characters

I'm moving a site from Interspire to WooCommerce and need to sort some htacess redirects but I'm having issues with special characters. For example I need...

/products/12"-x-1"-White%7B47%7DRed-Numbered-1%252d24-Nativity-Design-%252d-%28Pack-6%29.html

to redirect to

/shop/12-x-1-whitered-numbered-1-24-nativity-design-pack-6/

but I can't get it to work seemingly due to the special characters (ie. all other 'normal' redirects are working OK).

Anyone got any ideas how to handle the special characters (") and possibly the % stuff?!

  • Rupok
    • Support Ninja

    Hi Jon,

    Thanks for asking. I searched for a solution and it seems like you can escape a special character in regex rules by adding a backslash (\) before each character. The Apache documentation says:
    As of Apache 1.3.20, special characters in TestString and Substitution strings can be escaped (that is, treated as normal characters without their usual special meaning) by prefixing them with a backslash ('\') character. In other words, you can include an actual dollar-sign character in a Substitution string by using '\$'; this keeps mod_rewrite from trying to treat it as a backreference.

    Source: http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewriteflags

    Can you please try this and let us know how it goes? I'm looking forward to hearing from you and resolving this issue as soon as possible.

    Have a nice day. Cheers!
    Rupok

  • Jon
    • The Incredible Code Injector

    Thanks for this, not quite there yet though. I tried this...

    /products/12\%22-x-1\%22-White\%7B47%7DRed-Numbered-1\%252d24-Nativity-Design-\%252d-%28Pack-6\%29.html

    and various other combinations, but it's still not working. I'm getting confused about whether I just add a \ or \% and how many I need to add – any chance you could hold my hand a bit more?!

  • Kasia Swiderska
    • Support nomad

    Hello Jon,

    You need to escape all the characters. And I see in your example that you missed two % chars. So example from you first question:
    /products/12"-x-1"-White%7B47%7DRed-Numbered-1%252d24-Nativity-Design-%252d-%28Pack-6%29.html

    should look like that

    /products/12\"-x-1\"-White\%7B47\%7DRed-Numbered-1\%252d24-Nativity-Design-\%252d-\%28Pack-6\%29.html

    Let me know if this works

    kind regards,
    Kasia

  • Jon
    • The Incredible Code Injector

    I'm sorry, I still can't get this to work. I don't know if I"m doing anything else wrong. I've got this line in my .htaccess...

    `RedirectMatch permanent ^/products/12\"-x-1\"-White\%7B47\%7DRed-Numbered-1\%252d24-Nativity-Design-\%252d-\%28Pack-6\%29.html /shop/12-x-1-WhiteRed-Numbered-1-Nativity-Design-Pack-6

    Which is working for 'normal' URLs.

  • Jon
    • The Incredible Code Injector

    Sorry, not sure how to add code properly...

    RedirectMatch permanent ^/products/12\"-x-1\"-White\%7B47\%7DRed-Numbered-1\%252d24-Nativity-Design-\%252d-\%28Pack-6\%29.html /shop/12-x-1-WhiteRed-Numbered-1-Nativity-Design-Pack-6

  • Lindeni Mahlalela
    • Recruit

    Hello Jon,

    I hope you are having a great day so far. Thank you for your patience while we try to resolve this.

    If I am not wrong most parts of your url is url-encoded meaning some special characters have been replaced with the strings containing the dollar sign. One thing that I noticed is that some string a re double encoded like for example if you take the string %252d and decode it, it translates to %2d, if you decode that then it translates to '-'.

    So as you can see the url may not match unless you have a special reason why the url is encoded twice. So I suggest you get the original url un-encoded and encode it once also encoding the double quotes. This way you have a complete url encoded once so that when its decoded, it will translate to the required characters instead of another decoded string.

    I'm getting confused about whether I just add a \ or \% and how many I need to add

    When escaping the special characters with the back slash you should add only one backslash '\' in front of the character you want to escape like so ' \" ' for the double quote. With regards to the percentage sign, I think most of them are part of the encoded url and need not to be escaped. You may escape them if they are not part of the decode like "%2d" which will be decoded to "-", if you escape that then decoding it will result in some funny string which will prevent your code to work.

    If you can, please send us an original link (un-encoded) and the final link that this should be redirected to so that we can see a bigger picture of what you want to do.

    Another way you can do this, is if you have a few of those urls with special characters you can write special conditions for them with the RewriteCond and RewriteRule to match the specific url with special characters and redirect to the desired url using the rewrite rule. Again, it would really help if we have a copy of your .htaccess file and the exact original url you want to redirect so that we can play with it on our installs and we may find a solution and share it with you.

    If this does not work, please share your htaccess and the exat urls from the old website and the links they should redirect to in your new website. This will help us understand your whole setup and put us in a position to give you solid advice.

    I hope you find this useful.

    Cheers,
    Mahlamusa

  • Lindeni Mahlalela
    • Recruit

    Hey Jon,

    Sorry I forgot to mention that you can try this tool http://www.url-encode-decode.com/, to decode or encode your urls and see if they decode to the original urls or not. If you use this tool to decode a url and the result is not the original from your old website then the problem why the redirects don''t work is that the urls are double encoded.

    I need that case you should use the urls encoded once in your .htaccess file.

    I hope that helps.

    Cheers,
    Mahlamusa

  • Jon
    • The Incredible Code Injector

    Thanks for your help (and sorry for the delay in replying). I can't do anything with the old URLs as they were from an old version of the site which we can't edit anymore. The reason for the redirects is to try and ensure anyone who had bookmarks saved is still redirected to the new product. All I have is the old site map which gives eg.

    https://www.church-supplies.co.uk/products/12%22-x-1%22-White%7B47%7DRed-Numbered-1%252d24-Nativity-Design-%252d-%28Pack-6%29.html

    as the URL which we want to point to...

    https://www.church-supplies.co.uk/shop/12-x-1-whitered-numbered-1-24-nativity-design-pack-6/

    Is that helpful? I'm attaching my current .htaccess file which contains some experiments on this URL around line 146.

  • Jon
    • The Incredible Code Injector

    Thanks for this. I'm still struggling to get this working but I think I may have to chalk it up as a fail. As the new site has been up for a while it's becoming less crucial and I'm not sure it's worth the time it's taking. It's been a really useful process though to help me work out some of these issues.

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.