mod_rewrite 返回 302 找到

发布于 2024-09-30 16:05:58 字数 441 浏览 1 评论 0原文

我有一个 apache 2.2 并且我正在使用 mod_rewrite。我的目标是转发来自 http://localhost:80/AAhttp://localhost:8090/BB。所以我有一个简单的规则

RewriteRule http://localhost:80/AA http://localhost:8090/BB

我的问题是客户端收到“302 Found”。我希望 RewriteRule 能够 将流量从 AA 转发到 BB,最后 BB 将响应发送给我的客户端。

我的配置有问题吗?

I have an apache 2.2 and I am using mod_rewrite. My objective is to forward traffic from
http://localhost:80/AA to http://localhost:8090/BB. So I have a simple rule

RewriteRule http://localhost:80/AA http://localhost:8090/BB

My problem is that the client receives "302 Found". I was hoping that the RewriteRule will
forward the traffic from AA to BB and finally the BB will send the response to my client.

Is there a problem with my configuration?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

病女 2024-10-07 16:05:58

请参阅 RewriteRule 的文档

你想要做的是添加 R标记您的规则,用于重定向。

RewriteRule http://localhost:80/AA http://localhost:8090/BB [R=permanent,L]

L 表示最后一条规则。

See the documentation for RewriteRule

What you wanna do is add the R flag to your rule, for redirection.

RewriteRule http://localhost:80/AA http://localhost:8090/BB [R=permanent,L]

L for last rule.

长伴 2024-10-07 16:05:58

RewriteRule 的左侧或模式应该只是您要匹配的文件名,例如 ^/AA$。如果您还想确保主机和端口与特定值匹配,则应设置 RewriteCond 以匹配 %{HTTP_HOST}%{SERVER_PORT}。然而,后者可能是不必要的,除非您的 Web 服务器配置为多个域和端口。

这是未经测试的,但类似这样:

RewriteCond %{HTTP_HOST}   ^localhost
RewriteCond %{SERVER_PORT} ^80$
RewriteRule ^/AA$          http://localhost:8090/BB

我建议您先尝试一下:

RewriteRule ^/AA$          http://localhost:8090/BB

如果需要,请添加 RewriteCond。

根据评论进行编辑:如果您想避免“302”响应,则无法针对您所描述的情况执行此操作。当您使用 mod_rewrite 从一台主机重定向到另一台主机时,您将收到发送到浏览器的两个响应。第一个是 302,它告诉浏览器转到第二个 URL。第二个响应应该是 200。

使用 mod_rewrite,如果重写是在同一服务器和端口上从一页到另一页,则可以避免中间的重定向。在这种情况下,重写是内部的,即使请求是针对 AA,Web 服务器也可以使用 BB 页面进行响应。如果您使用不同的服务器或端口,则 AA 的 Web 服务器无权访问页面 BB,因此它会重定向到有权访问的服务器进行响应。

如果您可以重新配置站点以对 AA 和 BB 使用相同的端口,则可以使其正常工作。如果没有,我想你也许可以用 mod_proxy。我从未使用过 mod_proxy 所以我不确定你需要做什么。

The left side, or pattern, of your RewriteRule should just be the filename you want to match, e.g. ^/AA$. If you also want to make sure the host and port match specific values, you should set up RewriteCond's to match %{HTTP_HOST} and %{SERVER_PORT}. However the latter may be unnecessary unless your web server is configured for multiple domains and ports.

This is untested, but something like this:

RewriteCond %{HTTP_HOST}   ^localhost
RewriteCond %{SERVER_PORT} ^80$
RewriteRule ^/AA$          http://localhost:8090/BB

I would recommend that you try just this first:

RewriteRule ^/AA$          http://localhost:8090/BB

and add the RewriteCond's if you need them.

Edit based on comments: If you are trying to avoid the "302" response, you cannot do that for the situation you describe. When you use mod_rewrite to redirect from one host to another, you will get two responses sent to your browser. The first is 302, which tells the browser to go to the second URL. The second response should be 200.

With mod_rewrite, you can avoid the redirect in the middle if the rewrite is from one page to another on the same server and port. In that case, the rewrite is internal and the web server can respond with the BB page even though the request is for AA. If you use a different server or port, the web server for AA does not have access to the page BB, so it responds with a redirect to the server that does have access.

If you can reconfigure your site to use the same port for AA and BB, you can make it work. If not, I think you might be able to do what you want with mod_proxy. I have never used mod_proxy so I am not sure what you would need to do.

水溶 2024-10-07 16:05:58

如果您希望转发流量,我想 mod_rewrite 无法做到这一点。您最好使用 mod_proxyhttp:// httpd.apache.org/docs/2.0/mod/mod_proxy.html#proxypassreverse

文档中的示例:

ProxyRequests Off

<Proxy *>
   Order deny,allow
  Allow from all
</Proxy>

ProxyPass /foo http://foo.example.com/bar
ProxyPassReverse /foo http://foo.example.com/bar

If you wish to forward traffic, I guess mod_rewrite can't do that. You should preferably use mod_proxy: http://httpd.apache.org/docs/2.0/mod/mod_proxy.html#proxypassreverse

Example from the documentation:

ProxyRequests Off

<Proxy *>
   Order deny,allow
  Allow from all
</Proxy>

ProxyPass /foo http://foo.example.com/bar
ProxyPassReverse /foo http://foo.example.com/bar
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文