RewriteRule with an IPv6 IP address does not work

2020-07-27 04:19发布

问题:

I am having problems to proxy requestions inside a IPv6 address.

On .htaccess/apache2.conf, I have it:

RewriteRule (.*) http://18.4.15.8:80/path/$1 [P,L]

It works fine, because it is a IPv4 address. But, it does not works:

RewriteRule (.*) http://[27:ec:20:22:15::d3]:80/path/$1 [P,L]

Why?

Error Log:

Proxy Error

Your browser sent a request that this server could not understand. The proxy server could not handle the request GET /index.html.

Reason: URI cannot be parsed: http://%5b27:ec:20:22:15::d3%5d:80/path/index.html

回答1:

It's because of [ & ]. Thay are usually not allowed in a URL and so the server is escaping it to %b5and %5d. To prevent this use the NE Flag in your Rewrite Rule:

RewriteRule (.*) http://[27:ec:20:22:15::d3]:80/path/$1 [NE,P,L]

https://httpd.apache.org/docs/2.4/rewrite/flags.html#flag_ne