Apache 重写规则更改了不应该更改的内容

Apache rewrite rule changing things that it shouldn't

提问人:ralmond 提问时间:10/21/2023 最后编辑:Stephen Ostermillerralmond 更新时间:10/21/2023 访问量:19

问:

我想设置一个 Apache 服务器,以便它将请求重定向到在端口 3838 上运行的闪亮服务器。其他网页应转到正常的代理服务器。https://example.edu/rdemos/(.*)

所以我在我的指令中有以下指令:ssl.conf

RewriteEngine on
RewriteRule ^/PP/(.*) https://example.com/PhysicsPlayground/$1

<Location /rdemos>
  RedirectMatch permanent ^/rdemos$ /rdemos/
  RewriteEngine on
  RewriteCond %{HTTP:Upgrade} =websocket
  RewriteRule /rdemos/(.*) ws://localhost:3838/$1 [P,L]
  RewriteCond %{HTTP:Upgrade} !=websocket
  RewriteRule /rdemos/(.*) http://localhost:3838/$1 [P,L]
  ProxyPass http://localhost:3838/
  ProxyPassReverse http://localhost:3838/
  # Header edit Location ^/ /rdemos/                                                                                          
</Location>

这会正确地重定向 下的文件,因此会按预期进入闪亮的服务器(端口 3838)。/rdemoshttps://example.com/rdemos/index.html

但是,其他文件会被随机重定向。所以被路由到(不存在)。https://example.com/PP/demo.htmlhttps://example.com/rdemos/PhysicsPlayground/demo.html

我做错了什么?

我正在使用 Apache/2.4.37 (Red Hat Enterprise Linux) OpenSSL/1.1.1k mod_fcgid/2.3.9 SVN/1.10.2 mod_wsgi/4.6.4 Python/3.6 mod_apreq2-20101207/2.8.1

Apache mod-rewrite

评论


答:

0赞 Stephen Ostermiller 10/21/2023 #1

我不会试图将mod_alias和mod_rewrite指令混为一谈。将所有内容转换为mod_rewrite,因为您需要使用它。

重定向重写规则应使用[R=301,L]

我不会尝试将重写规则与标志混合在一起。只需使用重写规则即可。ProxyPassP

在重写规则中使用它更具可移植性,因此它们无需修改即可工作。^/?.htaccess

您永远不需要指定多个。RewriteEngine on

ProxyPassReverse应指定子目录。

RewriteEngine on

RewriteRule ^/?PP/(.*) https://example.com/PhysicsPlayground/$1 [R=301,L]

RewriteRule ^/?rdemos$ /rdemos/ [R=301,L]

RewriteCond %{HTTP:Upgrade} =websocket
RewriteRule ^/?rdemos/(.*) ws://localhost:3838/$1 [P,L]

RewriteCond %{HTTP:Upgrade} !=websocket
RewriteRule ^/?rdemos/(.*) http://localhost:3838/$1 [P,L]

ProxyPassReverse /rdemos/ http://localhost:3838/