Get parts of DOCUMENT_URI using regex and SSI

I'm using Apache 2.4 and Server Side Includes.

When I use this:

<!--#echo var="DOCUMENT_URI" -->

It returns: /foo/bar/file.html

I want to capture the parts of the URI, i.e.:

  <!--#if expr='v("DOCUMENT_URI") =~ [\/\\](.+)[\/\\](.+)[\/\\](.+)[.]'-->
    <!--#set var='URI_PART1' value='$1' -->
    <!--#set var='URI_PART2' value='$2' -->
    <!--#set var='URI_PART3' value='$3' -->
  <!--#else -->
  <!--#endif -->

I have tried a lot of variations, but cannot get Apache to recognise the regex at all, in any way. I was expecting "foo", "bar" and "file" to be recorded but instead receive the error [an error occurred while processing this directive].

I know the syntax has changed since Apache 2.4 and the above is what I've come up with based on my understanding of the new syntax..

Any help appreciated!

Upvotes: 2

Views: 1310

Answers (2)

Sam Liddicott
Sam Liddicott

Reputation: 1406

This is reported as a bug, but un-resolved: https://bz.apache.org/bugzilla/show_bug.cgi?id=57448

There is a workaround as demonstrated in comment 8, to extract single values via $0, by adding this fragment:

&& $1 =~ /(.*)

which maps $1 to $0 as $0 is properly exported.

So you would have to extract your parts one at a time.

The other workaround is to enable and work with SSILegacyExprParser

Upvotes: 4

Ok so there is a lot of pre- Apache 2.4 documentation out there which is very confusing.

The answer is that you cannot isolate $1, $2, etc. parts your matched expression UNLESS it is to be used in another #if expression.

i.e. you cannot #set a new variable with $1 part of the string.

Not sure why this functionality is no longer possible, but yeah. There you go.

Upvotes: 0

Related Questions