Update
Thanks for all the replies so far. I'd like to make clear that the number after user
(e.g. user1) in my example is not significant. The user can be any alphanumeric name.
I want to make sure that if a directory is specified, we append "/faces/main.jsf" to the end of it.
For example
We want to URL to look like:
example.com/user1/faces/main.jsf
The system could append things like (which we must allow)
http://example.com/user1/faces/main.jsf;jsessionid=Z_DV-bY6MuQGlIvflPwgdKDk60cNzOkWXbC5G18ILrjR0jKoGWnd!-617980607
If a request looks like either of these:
example.com/user1
example.com/user1/
I'd like "/faces/main.jsf" to be appended to it. What's wrong with my rewrite rules?
<VirtualHost *:80>
ServerName example.com
ProxyPass / http://1.example.com:8888/
ProxyPassReverse / http://1.example.com:8888/
RewriteEngine On
# RewriteLog /tmp/rewrite.log
RewriteCond %{HTTP_HOST} !^example.com$
RewriteRule !^/(.) http://example.com/$1 [L,R]
RewriteCond %{REQUEST_URI} !^/(.+/)faces/main.jsf.*
RewriteRule !^(.+)/faces/main.jsf.*$ $1/faces/main.jsf [L,R]
</VirtualHost>
Try this
This will work for user0..user9 but not user10. If that is an issue, change the last rewrite rule to use ^(user\d+/)$
I am assuming that you want to proxy all requests to the other host you are using in the proxy configuration. Therefore you should combine both tasks into a single set of directives like so:
If you want to rewrite only specific URLs such as "/userX", you can easily adapt the above regular expression.
eg:
http://example.com/user2 --> http://1.example.com:8888/user2
http://example.com/user1 --> http://1.example.com:8888/user1/faces/main.jsf
http://example.com/user1/ --> http://1.example.com:8888/user1/faces/main.jsf
http://example.com/user1;jsessionid=Z_DV-bY6MuQGlIvflPwgdKDk60cNzOkWXbC5G18ILrjR0jKoGWnd!-617980607
-->
http://1.example.com:8888/user1/faces/main.jsf;jsessionid=Z_DV-bY6MuQGlIvflPwgdKDk60cNzOkWXbC5G18ILrjR0jKoGWnd!-617980607
The [P] will proxy the requests to and from your backend.
That should be it unless you actually intended /user2 --> /user9 to also work, if so just say exactly what you're looking for so I can give you the right rules :-)
For the above to work you will of course need the proxy, proxy_http and rewrite mods enabled.
This should work for a first level directory with alphanumeric characters.
You asked what's wrong with your rewrite rules. Look at these two lines in your configuration:
Your RewriteCond rule will match if the url doesn't already have the faces/main.jsf bit. That looks fine. Your RewriteRule should not then be trying to match /faces/main.jsf. I'm really not sure what you expect to happen with the leading '!' on that pattern.
Maybe you want something like this:
Your construction with the ';' is quite unusual, and if I saw it in a system I'd be double checking that that's actually what's wanted, (though that was not your question). A ';' after a '?' can be used in the same manner as a '&', which reduces the need for escaping in html. Was that what you meant? If so, then you should know that the part of the URL after the '?' is not part of what gets fed to RewriteRule.