regexapacheregex-groupssirequest-uri

How to use Regex on Apache REQUEST_URI?


Currently using Apache 2.4 with SSI Legacy Parser On and trying to use Regex code to obtain the results I am looking for.

<!--#set var="test" value=$REQUEST_URI -->
<!--#echo var="test" -->

The example result I get from the above code is:

/path1/path2/path3/filename.html

/path1/path2/path3/filename.html?id=2019

The example result I would like to get is:

/path1/path2/path3/

Anything after the last forward slash removed regardless of the number of paths there may be.

Is there a regex code or something I could use to do this?


Solution

  • Sure! Here we can simply add a slash boundary and swipe everything from beginning to the last slash:

    (.*)\/
    

    The expression could stop right there, and it can be simply called using $1. However, we can also add more boundaries to it, if you wish, such as start and end chars:

    ^(.*)\/.*$
    

    enter image description here

    RegEx

    If this wasn't your desired expression, you can modify/change your expressions in regex101.com.

    RegEx Circuit

    You can also visualize your expressions in jex.im:

    enter image description here

    JavaScript Demo

    const regex = /^(.*)\/.*$/gm;
    const str = `/path1/path2/path3/filename.html
    /path1/path2/path3/filename.html?id=2019
    /path1/path2/path3/path4/filename.html?id=2019
    /path1/path2/path3/path4/path5/filename.html?id=2019
    
    `;
    const subst = `$1`;
    
    // The substituted value will be contained in the result variable
    const result = str.replace(regex, subst);
    
    console.log('Substitution result: ', result);