重写规则:追加URL的path

更新

感谢所有迄今为止的答复。 我想清楚地说明,在我的例子中user (例如user1)之后的数字并不重要。 用户可以是任何字母数字名称。


我想确保如果指定了一个目录,我们在它的末尾添加“/faces/main.jsf”。

例如

我们想要URL看起来像:

 example.com/user1/faces/main.jsf 

该系统可以附加像(我们必须允许的)

  http://example.com/user1/faces/main.jsf;jsessionid=Z_DV-bY6MuQGlIvflPwgdKDk60cNzOkWXbC5G18ILrjR0jKoGWnd!-617980607 

如果请求看起来像这样:

 example.com/user1 example.com/user1/ 

我想要“/faces/main.jsf”被追加到它。 我的重写规则有什么问题?

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

我假设你想代理所有的请求到你在代理configuration中使用的另一台主机。 因此,您应该将这两个任务组合成一组指令,如下所示:

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

如果只想重写“/ userX”等特定的URL,则可以轻松地调整上述正则expression式。

尝试这个

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

这将为user0..user9,但不是user10。 如果这是一个问题,请将上次重写规则更改为使用^(user \ d + /)$

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

例如:

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

[P]将代理请求到你的后端。

这应该是它,除非你真的打算/用户2 – > /用户9也工作,如果是这样只是说出你正在寻找什么,所以我可以给你正确的规则:-)

对于上面的工作,你当然需要代理,proxy_http和重写mods启用。

这应该适用于带有字母数字字符的第一级目录。

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

你问你的重写规则有什么问题。 看看你的configuration中的这两行:

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

如果URL尚未包含faces / main.jsf位,则您的RewriteCond规则将匹配。 这看起来很好。 你的RewriteRule不应该试图匹配/faces/main.jsf。 我真的不确定你期望会发生什么,领先的'!' 在那个模式。

也许你想要这样的东西:

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

你的build筑与';' 是非常不寻常的,如果我在一个系统中看到它,我会仔细检查这是否真正需要什么,(但这不是你的问题)。 一个 ';' 之后 '?' 可以像'&'一样使用,这样就减less了在html中转义的需要。 那是你的意思吗? 如果是这样,那么你应该知道,'?'后的URL部分 不是什么得到喂养RewriteRule的一部分。