1

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?

1 Answer 1

1

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);

1
  • 1
    You got me in the right direction.....got it based off what you supplied. <!--#if expr="$REQUEST_URI = /^(.*)\/.*$/" --> <!--#set var="abspath" value="$1" --> <!--#endif --> <!--#include virtual="${abspath}/whateverpage.shtml --> <!--#include virtual="${abspath}/whateverFolder/whateverpage.shtml -->
    – SteveL
    Commented May 19, 2019 at 12:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.