【问题标题】:How to forward requests to another URL?如何将请求转发到另一个 URL?
【发布时间】:2013-11-08 23:28:27
【问题描述】:

目前,我的 httpd.conf 文件中有以下规则,用于将所有请求从端口 80 转发到端口 8080,以由 GlassFish 应用服务器提供服务:

<VirtualHost *:80>
    ServerAdmin admin@myserver.com
    ServerName myserver.com
    ProxyPreserveHost On

    # setup the proxy
    <Proxy *>
        Order allow,deny
        Allow from all
    </Proxy>
    ProxyPass / http://localhost:8080/
    ProxyPassReverse / http://localhost:8080/
</VirtualHost>

现在,我需要添加一条规则,以便所有对http://myserver.com/ 的请求都将转发到http://myserver.com/page/index.html,并且该URL 在客户端浏览器上仍应显示为http://myserver.com/。我尝试在上面的VirtualHost中添加以下规则:

RewriteEngine On
RewriteRule http://myserver.com/ http://myserver.com/page/index.html

RewriteEngine On
RewriteRule ^/ http://myserver.com/page/index.html

RewriteEngine On
RewriteRule ^/index.html http://myserver.com/page/index.html

但是,当我转到http://myserver.com/ 时,浏览器出现此错误:This webpage has a redirect loop。只有转到http://myserver.com/index.html,第三条规则才有效。

我在为 Apache 编写规则方面完全是个菜鸟。因此,如果你能告诉我我在这里做错了什么,我将非常感激:)。

更新:

以下规则完美运行:

RewriteEngine On
RewriteRule ^/$ /page/index.html [R]

【问题讨论】:

    标签: apache mod-rewrite url-rewriting forward rule


    【解决方案1】:

    您需要添加一个$ 表示URI 的结尾:

    RewriteEngine On
    RewriteRule ^/$ http://myserver.com/page/index.html
    
    ProxyPass / http://localhost:8080/
    ProxyPassReverse / http://localhost:8080/
    

    没有$,正则表达式^/匹配/page/index.html,这将导致它再次重定向,它会再次匹配,再次重定向,等等。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-06-07
      • 2017-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多