Search code examples
javaregextuckey-urlrewrite-filter

Dynamic URL with tuckey urlrewritefilter


I have multiple links like

- http://example.com/link1/title-with-detail
- http://example.com/link2/title-with-detail
- http://example.com/link3/title-with-detail

and so on...

I have a tuckey rule which is working perfectly fine:

    <rule>
        <from>^/([^/]+)/([^/]+)/type/([a-z]+)$</from>
        <to>/page?slug=$2&amp;type=$3</to>
    </rule>

For link like - http://example.com/link1/title-with-detail/type/text

But when I use this rule, i.e.

<rule>              
        <from>^/([^/]+)/([^/]+)$</from>
        <to>/page?slug=$2</to>
</rule>

my page breaks, because in the slug is getting parameters such as styles.css, functions.js, etc. For this I tried to use conditions of tuckey to exclude the text which is equal to the text mentioned before, i.e. js, css, png etc. But I don't know what exact condition to form, as I am newbie to regular expressions. Please guide.


Solution

  • You can set up a condition like this:

    <condition type="request-uri">^(?!.*\.(?:jpg|png|css|js)$).*$</condition>
    

    Accoring to the specifiactions, you can check the request-uri type in the condition so as to check what it ends with:

    request-uri - Returns the part of this request's URL from the protocol name up to the query string in the first line of the HTTP request

    The negative look-ahead (?!.*\.(?:jpg|png|css|js)$) actually checks if the URI does not end with any of the extensions listed in the non-capturing group. You can add more extensions that you want to exclude after | symbol.