Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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>
share|improve this question
    
Try using htaccess.madewithlove.be to investigate. –  Schrute Jul 1 at 17:52
1  
what about the proxy configuration? When all requests are supposed to proxy to 1.example.com, you should take that into account in the rewrites. –  ofrommel Jul 3 at 14:28
    
I've updated my post to specify that the number after the user (e.g. user1) is not significant. It can be any user name. –  Felipe Alvarez Jul 4 at 6:13
    
So will all user names be prefixed with 'user' as in userFelipeAlvarez or on the other hand is the first filepath always the user name? I'm afraid your post update hasn't quite made it clear yet. Until that is accurately specified the rewrite rule can't be properly written. –  Ian Macintosh Jul 8 at 5:43
    
user is arbitrary alphanumeric string. –  Felipe Alvarez Jul 9 at 1:44

5 Answers 5

up vote 1 down vote accepted
+50

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:

<VirtualHost *:80>
    ServerName          example.com
    RewriteEngine       On

    RewriteCond %{REQUEST_URI}  !^/(.+/)faces/main.jsf
    RewriteRule ^(.+?)(;.*) http://1.example.com:8888/$1/faces/main.jsf$2 [P]
    ProxyPassReverse / http://1.example.com:8888/
</VirtualHost>

If you want to rewrite only specific URLs such as "/userX", you can easily adapt the above regular expression.

share|improve this answer
    
Does this cater for http://example.com/user to redirect to http://example.com/user/faces/main.jsf ? –  Felipe Alvarez Jul 4 at 6:13
    
Sorry, I misread your requirements. Updated my answer accordingly. –  ofrommel Jul 4 at 6:34

Try this

<VirtualHost *:80>
    ServerName          example.com
    ProxyPass           /       http://1.example.com:8888/
    ProxyPassReverse    /       http://1.example.com:8888/
    RewriteEngine       On
    # RewriteLog          /tmp/rewrite.log

    RewriteBase /

    RewriteCond %{HTTP_HOST} !^example.com$
    RewriteRule !^/(.) http://example.com/$1 [L,R]

    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^(user\d/)$  $1faces/main.jsf [L,R]
</VirtualHost>

This will work for user0..user9 but not user10. If that is an issue, change the last rewrite rule to use ^(user\d+/)$

share|improve this answer
<VirtualHost *:80>
 ServerName example.com

 RewriteEngine On
 RewriteRule ^/user1/(.*) http://1.example.com:8888/user1/faces/main.jsf$1 [L,P]
 RewriteRule ^/user1(.*) http://1.example.com:8888/user1/faces/main.jsf$1 [L,P]
 RewriteRule ^/(.*) http://1.example.com:8888/$1 [L,P]
</VirtualHost>

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.

share|improve this answer

This should work for a first level directory with alphanumeric characters.

<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]

    RewriteRule ^/([a-z0-9]+)/?$  /$1/faces/main.jsf [L,R,NC]
</VirtualHost>
share|improve this answer

You asked what's wrong with your rewrite rules. Look at these two lines in your configuration:

RewriteCond %{REQUEST_URI}  !^/(.+/)faces/main.jsf.*
RewriteRule !^(.+)/faces/main.jsf.*$  $1/faces/main.jsf [L,R]

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:

RewriteCond %{REQUEST_URI}  !^/(.+/)faces/main.jsf.*
RewriteRule ([^;]*)(;.*)? $1/faces/main.jsf$2 [L,R]]

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.