Question

Is it possible to use a backreference in the middle of the RewriteRule pattern?

I envision this as such:

RewriteRule  ^(.)([a-z]*)$1([a-z]*)  $2-$3
                          ^
     (note, this fellow, here, refers to that first "(.)")

If the received url is XboopXdoop, the result would of course be boop-doop.

I am attempting to use this to specify a delimiter at the beginning of the incoming url that can be used to parse the rest of the string, without forcing the use of a specific character as that delimiter.

Thank you.

Was it helpful?

Solution

$1 works on the right side (rewrite), but not in the regex. You need to use \1.

Try:

RewriteRule ^(.)([a-z]+)\1([a-z]+) $2-$3

I ran into a bizarre edge case with the * where it split based on the second character of the string, and not the second. XtestingXtest resulted in es-ing ... so yeah, not sure what was happening there. If I use a + it works fine.

Also, since * and + are greedy, if you have multiple delimiter characters, it will split on the last occurrence of the character:

XbaseXtest  -> base-test 
XbaseXteXst -> baseXte-st
XbaseXtestX -> baseXtest-
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top