Search code examples
url-rewritingfilterseotuckey-urlrewrite-filter

URL Rewrite Filter not working with query parameters containing special characters


I'm using URL Rewrite Filter to forward some ugly URLs to pretty Urls. Referring to Conditions Based On URL Parameters, I’ve done something using UrlRewriteFilter which is actually required to make my site Google crawl-able. Here’s how it goes.

<rule enabled="true">
        <note>
            The rule means that requests to /test/status/ will be redirected to /rewrite-status
            the url will be rewritten.
        </note>
        <condition type="parameter" name="_escaped_fragment_" operator="equal">(apple|kiwi|orange)</condition>
            <from>^/mysite/(.+)/(.*)$</from>
            <to type="redirect">/mysite/%{parameter:_escaped_fragment_}</to>
    </rule>

It fails throwing java.lang.IllegalArgumentException: URLDecoder: Illegal hex characters in escape (%) pattern – %%7. As my variable contains underscores (_escaped_fragment_), where in it works fine with a parameter variable called ‘friuit’. Please help me get out of it.


Solution

  • <rule>
    <condition name="_escaped_fragment_" type="parameter" operator="equal">(apple|kiwi|orange)</condition>
    <to type="redirect">/mysite/%1</to>
    </rule>
    

    Using the value of a query parameter with %{parameter:_escaped_fragment_} would work only with words containing no special characters. where in %1 (that is % followed by query parameter index) will work for any, which solved my problem.