为 Apache 2.2 和 JBoss 4.2.3 配置 mod_rewrite 和 mod_jk
我的问题如下:我的 JBoss 4.2.3 应用程序服务器与 AJP 1.3 连接器在 Windows 下的一台主机上运行(我的测试环境为 192.168.1.2),Apache 2.2.14 在另一台 FreeBSD 机器(192.168.1.10)上运行。 Apache 充当所有请求的“前门”,并通过 mod_jk 将它们发送到 JBoss。一切都工作正常,直到我必须做一些 SEO 优化。这些优化包括 SEF url,因此我决定使用 Apache 的 mod_rewrite 来更改请求,然后再将请求发送到 JBoss。基本上,我需要实现 2 条规则:
- 重定向旧规则,例如“http://hostname/directory/ ” 到“http://hostname/”,并具有永久重定向
- 转发网址,例如“http://hostname/wtf/123/" 到 "http://hostname/wtf/view.htm?id=123" 这样最终用户就不会看到“丑陋”的 URL(实际重写) 。
这是我的测试虚拟主机的 Apache 配置:
<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot "/usr/local/www/dummy"
ServerName 192.168.1.10
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule /directory/(.*) /$1 [R=permanent,L]
RewriteRule ^/([^/]+)/([0-9]+)/?$ /$1/view.htm?id=$2
</IfModule>
JkMount /* jsp-hostname
ErrorLog "/var/log/dummy-host.example.com-error_log"
CustomLog "/var/log/dummy-host.example.com-access_log" common
</VirtualHost>
问题是第二个重写规则不起作用。请求不变地传递到 JBoss,因此我收到 Tomcat 404 错误。但是,如果我将重定向标志添加到第二条规则中,就像
RewriteRule ^/([^/]+)/([0-9]+)/?$ /$1/view.htm?id=$2 [R,L]
它的魅力一样。但重定向不是我在这里需要的:)。我怀疑问题在于请求被转发到另一个主机(192.168.1.2),但我真的不知道如何使其工作。任何帮助将不胜感激:)
My problem is as follows: I have JBoss 4.2.3 application server with AJP 1.3 connector running on one host under Windows (192.168.1.2 for my test environment) and Apache 2.2.14 running on another FreeBSD box (192.168.1.10). Apache acts as a "front gate" for all requests and sends them to JBoss via mod_jk. Everything was working fine until I had to do some SEO optimizations. These optimizations include SEF urls, so i decided to use mod_rewrite for Apache to alter requests before they are sent to JBoss. Basically, I nedd to implement 2 rules:
- Redirect old rules like "http://hostname/directory/" to "http://hostname/" with permanent redirect
- Forward urls like "http://hostname/wtf/123/" to "http://hostname/wtf/view.htm?id=123" so that end user doesn't see the "ugly" URL (the actual rewrite).
Here is my Apache config for test virtual host:
<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot "/usr/local/www/dummy"
ServerName 192.168.1.10
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule /directory/(.*) /$1 [R=permanent,L]
RewriteRule ^/([^/]+)/([0-9]+)/?$ /$1/view.htm?id=$2
</IfModule>
JkMount /* jsp-hostname
ErrorLog "/var/log/dummy-host.example.com-error_log"
CustomLog "/var/log/dummy-host.example.com-access_log" common
</VirtualHost>
The problem is that second rewrite rule doesn't work. Requests slip through to JBoss unchanged, so I get Tomcat 404 error. But if I add redirect flag to the second rule like
RewriteRule ^/([^/]+)/([0-9]+)/?$ /$1/view.htm?id=$2 [R,L]
it works like a charm. But redirect is not what I need here :) . I suspect that the problem is that requests are forwarded to the another host (192.168.1.2), but I really don't have any idea on how to make it work. Any help would be appreciated :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的第二个重写规则不起作用的原因是您使用了“?”在您的 URI 定义中,并且 URI 定义从不包含分隔符“?”。
所以只需使用没有它的重写规则即可。例如。
The reason your second rewrite rule doesn't work is that you use the '?' in your URI definition and the URI definition never contains the separator '?'.
So simply use the rewrite rules without it. eg.
简单地说,这是行不通的,因为第一个
RewriteRule
末尾有[L]
,这意味着它是要处理的最后一个规则。Simply, doesn't works because the first
RewriteRule
has the[L]
at the end, which means is the last rule to process.