1

EDIT This works, needed to use % in place of $ for capture groups:

RewriteRule (.*) http://localhost:8081/%1/%{TIME_YEAR}1101%2 [P]

Having a hard time sorting this out.

Need a date string conditionally injected into specific URIs (for proxy to Jetty app server), the condition being, if no 8-digit date string already present, inject it.

The hack I came up with is:

RewriteCond %{REQUEST_URI} ^/(foo|bar|baz)(.*)
RewriteCond %2 !/(\d{8})(.*)
RewriteRule (.*) http://localhost:8081$1/%{TIME_YEAR}1101$2 [P]

This works to some degree, but not all in that $2 is empty, $1 matches the entire URI.

Basically what winds up happening is a request for /foo/2 gets rewritten to /foo/2/20121101, while what I need to have happen is /foo/20121101/2

So, given that $1 matches the entire URI, how can I get it to match only foo|bar|baz, leaving $2 as the URI remainder with which I can then sandwich in the date?

Also, is this an horribly inefficient approach? Apache docs usually indicate chaining [OR]s so not sure if straight regex is the way to go (certainly more concise). On Apache 2.4 so hoping to actually benefit from the speed enhancements (vs. shooting self in the foot)

Thanks

user9517
  • 114,104
  • 20
  • 206
  • 289
virtualeyes
  • 665
  • 3
  • 10
  • 28

1 Answers1

1

If I understand what you're doing right, which I may not, I'd say it can be simpler:

RewriteCond %{REQUEST_URI} !/\d{8}(/|$)
RewriteRule ^/(foo|bar|baz)(/?.*)$ http://localhost:8081/$1/%{TIME_YEAR}1101$2 [P]
Shane Madden
  • 112,982
  • 12
  • 174
  • 248
  • got it working, but this is a clear example of the benefit of having another perspective: you break out of 1-way-thinking ;-) Now, which is more efficient, your approach where you first check if URI contains date param, or mine where I first check the target pages? (keep in mind that only a small set of URIs require date injection) – virtualeyes Apr 25 '12 at 22:31
  • also, URI will be in form of /foo, /foo/id, /foo/id/id2, and date embedded versions that do not require date injection: /foo/date, /foo/date/id, /foo/date/id/id2. Need to handle trailing slash as well, but anyway, curious to hear your thoughts on chicken & the egg regex, I suspect it's more efficient to check for the pages first, and then check if uri contains date – virtualeyes Apr 25 '12 at 22:39
  • Edited to cover the `/foo` case - trailing slashes are already kept in the replacement string, since they're caught in the final match. Checking for the page first will be faster if the majority of requests aren't within the pages, but requires 3 matches instead of 2 for a request where it needs to take action. Checking for the date string first will be faster if the majority of requests will already have it. But unless the load on this site is enormous, then you should prefer a more readable configuration over the one that'll be faster by a tiny smidge. – Shane Madden Apr 26 '12 at 00:37
  • brilliant, this little nugget of gold "(/|$)", never occurred to me to match trailing slash and string termination in that way. Also, your approach is better: while date-enabled URIs are few, they receive most of the traffic; therefore, as you say, better to match twice than thrice ;-) – virtualeyes Apr 26 '12 at 08:01