apache mod_rewrite 代理 RewriteRule 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1806610/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 18:27:03  来源:igfitidea点击:

mod_rewrite proxy RewriteRule not working

apachemod-rewrite

提问by Lightbeard

I need to send all requests for the directory home/alphauser to backend/alphauser, but not requests to files inside the home/alphauser directory. For example:

我需要将对目录 home/alphauser 的所有请求发送到 backend/alphauser,而不是对 home/alphauser 目录中的文件的请求。例如:

http://home/alphauser            ->   http://backend/alphauser
http://home/alphauser/           ->   http://backend/alphauser
http://home/alphauser/icon.png   ->   http://home/alphauser/icon.png
http://home/alphauser/index.html ->   http://home/alphauser/index.html

I created an ".htaccess" file in the home/alphauser/ directory with the following:

我在 home/alphauser/ 目录中创建了一个“.htaccess”文件,内容如下:

RewriteEngine on
RewriteRule ^$ http://backend/alphauser [P]

mod_rewrite allows for access to files inside the home/alphauser/ directory as expected, but when the directory itself is requested either with or without the slash:

mod_rewrite 允许按预期访问 home/alphauser/ 目录中的文件,但是当请求目录本身时,带或不带斜杠:

http://home/alphauser
http://home/alphauser/

..the browser (firefox) presents a file download popup that states:

.. 浏览器 (firefox) 会显示一个文件下载弹出窗口,说明:

You have chosen to open a file which
is a: httpd/unix-directory

The contents of the file is the proper html from backend/alphauser (which is the url pattern to a JSP) so the payload returned is correct. It seems as though apache is sending back this strange mime type of "httpd/unix-directory"

该文件的内容是来自后端/alphauser 的正确 html(这是 JSP 的 url 模式),因此返回的有效负载是正确的。好像 apache 正在发回这种奇怪的 mime 类型的“httpd/unix-directory”

Help!

帮助!

采纳答案by Lightbeard

It turns out the problem had nothing to do with mod_rewrite. My backend was not sending a ContentType header at all. Once I set it to populate the ContentType as text/htmleverything worked.

事实证明,问题与 mod_rewrite 无关。我的后端根本没有发送 ContentType 标头。一旦我将其设置为在text/html一切正常时填充 ContentType 。

回答by RageZ

Robert,

罗伯特,

this seems to be weird to me

这对我来说似乎很奇怪

RewriteEngine on
RewriteRule ^$ http://backend/alphauser [P]

the regular expression you made basically match anything fair enougth but it would not pass the URI to the backend.

您制作的正则表达式基本上匹配任何公平的东西,但它不会将 URI 传递给后端。

RewriteEngine on
RewriteRule ^/alphauser/$ http://backend/alphauser [P]

or

或者

RewriteRule ^/$ http://backend/alphauser [P]

I am not 100 % sure how mod_rewrite behave in a .htaccess file

我不是 100% 确定 mod_rewrite 在 .htaccess 文件中的行为

This would make much sense it my opinion, also you have to make sure you have mod_proxyand mod_proxy_httpenabled or it won't work.

我认为这很有意义,您还必须确保已启用mod_proxymod_proxy_http启用,否则它将无法工作。