Apache CGI redirect to absolute URI doesn't wo

2019-07-21 02:26发布

问题:

I have Apache 2.2.13 running in console mode on Windows. I have made an executable that handles requests. In a certain case, when it detects a URL pointing to a directory but has not trailing slash, it tries to redirect to the same URL with the missing slash appended. The exit-code is set to 301. Strangely enough, having this in the response header doesn't work:

Location: /cgi-bin/mycgi.exe/something/

but this does:

Location: something/

Am I doing something wrong? Or did I discover a bug in Apache? (If so, where and how should I post it best?)

回答1:

Usually, 'Location' contains a full URL, including http: and the hostname. This is the case outlined by DVK.

Location: /cgi-bin/mycgi.exe/something/

This is actually something else: an internal redirect. It is defined by the CGI specification and works in some other server environments derived from CGI, such as PHP. When Location contains a 'virtual path', Apache serves up the page/script in that path straight away, without the browser knowing there was any kind of redirect.

Obviously that's not what you want as it makes no sense to do a 301 internal redirect when the browser will never see that it's a 301.

Location: something/

This, on the other hand, is nothing. It's not a full URL and it's not a virtual path as it doesn't begin with '/'. Apache doesn't know what to do with it, so it just guesses that it isn't a virtual path so spits it back to the browser with no further comment.

It's invalid to send this in a 'Location' header to a browser, but many of them will allow it anyway, which is why it appears to work. Really you should be passing the full URL:

Location: http://www.example.com/cgi-bin/mycgi.exe/something/


回答2:

1) Just to be clear, the redirection functionality (e.g. "what to do when seeing "Location: http response) is in your browser, not in Apache. I assume you know that but wanted to make sure it's clear. The reason it's relevant here is because as per the RFC, the address in 301 response needs to be "a single absolute URI". So your URI example is missing your domain name, e.g. needs to be http://your.web.server/cgi-bin/mycgi.exe/something/

While some web clients accept 301 with relative redirect, others do not.

2) Can you please specify exactly what you mean by "does not work" including client's error and any errors in Apache's log? Thanks

Also, please specify which of the following URLs work from the client and which do not:

  • http://your.web.server/cgi-bin/mycgi.exe/something/
  • http://your.web.server/something/
  • http://your.web.server/cgi-bin/mycgi.exe/something

Thanks