Search code examples
regextomcattuckey-urlrewrite-filter

Using Tuckey URL Rewrite to add file extension


I am using tuckey url rewrite filter, and I want to add the language extension (.cfm) to a clean url. (I know I am making the assumption that the last word in the path is always the script.)

For example, I want to add .cfm to

/someScript
/module/employee/anotherScript

and obviously not to paths that already have an extension (as defined as having a period)

/someScript.cfm
/module/employee/anotherScript.cfm

Here's the regex rule I have in mind:

<rule>
    <from>^((?!\.)[\w/])*$</from>
    <to type="redirect" last="true" qsappend="true">$0.cfm</to>
</rule>

Here's the problem. I also want to allow for an optional trailing slash so that the slash is removed and the file extension is added.

/someScript/
/module/employee/anotherScript/

How can I re-write my regex rule to ignore the optional trailing slash?


Solution

  • ^((?:(?!\.)[\w\/])*?)\/?$
    

    Try this.See demo.Replace by $1.cfm

    https://regex101.com/r/fA6wE2/6