Search code examples
regexapacheapache2mod-proxyapache2.2

Apache mod_proxy_html Substitute: how to re-use part of regex match? (regex variables?)


[Full disclosure: Cross-post between here and ServerFault, because I believe the audiences (server admins & devs) are distinct enough to warrant asking the question to both separately.]

Hi all,

Have a unique URL-rewriting situation in Apache.

I need to be able to take a URL that starts with

"\u002f[X]"

or

'\u002f[X]"

Where X is the rest of some URL, and substitute the text

"\u002fmeis2\u002f[X]

I'm not sure how the Regex works in Apache -- I think it's the same as Perl 5? -- but even then I'm a little unsure how this would be done. My hunch is that it has to do with Regex grouping and then using $1 to pull the variable out, but I'm entirely unfamiliar with this process in Apache.

Hoping someone can help -- thanks!


Solution

  • You are right. Group the text that you want to re-use with parens, and use $1 in the substitution. Use the following .htaccess file:

    RewriteEngine On
    RewriteRule ^\u002f(.*) /\u002fmeis2\u002f$1
    

    (I am not certain that mod_rewrite handles unicode escapes, but it seems so from your question.)