Forum Moderators: phranque

Message Too Old, No Replies

Internal rewrite to absolute URL on the same host results in redirect

The docs seem to suggest the "same" host should be stripped?

         

penders

1:46 pm on Mar 31, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Normally, if you specify an absolute URL (scheme + hostname) in the RewriteRule substitution when performing an internal rewrite (no R flag) then you'll implicitly get an external redirect.

However, the Apache docs for the RewriteRule directive states [httpd.apache.org]:

Absolute URL
If an absolute URL is specified, mod_rewrite checks to see whether the hostname matches the current host. If it does, the scheme and hostname are stripped out and the resulting path is treated as a URL-path. Otherwise, an external redirect is performed for the given URL. To force an external redirect back to the current host, see the [R] flag below.


But, in my experience, specifying an absolute URL in the substitution will always result in an external redirect, so what have I missed? What is the above referring to?

For example, when accessing "http://example.com/foo", the following will result in an implicit external redirect:

RewriteRule ^foo http://example.com/bar.php [L]


Why is "http://example.com" not stripped from the substitution?

not2easy

2:04 pm on Mar 31, 2015 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



If I understand this correctly you want a 301 permanent redirect, but the problem is the [L] tag doesn't have [301,L] so it defaults to the 302 temporary. It is the Apache default unless 301 is specified.

penders

2:46 pm on Mar 31, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



No, the intention is to have an internal rewrite. However, my interpretation of the Apache docs (quoted above) would seem to suggest that the following two directives (when the current host is "example.com" - the same host as specified in the substitution) would result in the same thing (an internal rewrite):


RewriteRule ^foo http://example.com/bar.php [L]
RewriteRule ^foo /bar.php [L]


However, the first results in an external redirect. Why is the scheme + hostname not stripped from the substitution like the docs seem to suggest? (Further more, don't the docs also imply that I would need to use the R flag in the first example to force an external redirect?)

aakk9999

2:59 pm on Mar 31, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



@penders, this is how I read it too, and they give these two different examples:

For RewriteBase /somepath and for Request GET /somepath/localpath/pathinfo

^localpath(.*) http://thishost/otherpath$1
Resulting substitution: /otherpath/pathinfo

^localpath(.*) http://otherhost/otherpath$1
Resulting substitution: http://otherhost/otherpath/pathinfo via external redirection

Perhaps the document is wrong?

penders

4:13 pm on Mar 31, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



(Thanks, I hadn't actually checked with the examples this time)...

So, yes, the examples appear to correlate with our interpretation of the docs, but that's not the way it seems to work in reality!? Incidentally the examples cover both httpd.conf and .htaccess and it appears that it should work this way in both.

Perhaps the document is wrong?


I would find this very hard to believe... for the description and examples, in possibly one of the most fingered chapters of the Apache docs in versions... 2.4, 2.2, 2.0 and 1.3 all to be wrong!?

However, the Apache 2.0 docs [httpd.apache.org] seem to be a bit more confusing/contradictory in this respect (the examples are, however, the same):

Note: Substitution of Absolute URLs
When you prefix a substitution field with http: //thishost[:thisport], mod_rewrite will automatically strip that out. This auto-reduction on URLs with an implicit external redirect is most useful in combination with a mapping-function which generates the hostname part.
Remember: An unconditional external redirect to your own server will not work with the prefix http: //thishost because of this feature. To achieve such a self-redirect, you have to use the R-flag.


Why does it say "implicit external redirect" (emphasis my own)? Unless maybe it is referring to what "looks like it could be" an external redirect? Although that is actually what seems to be happening, an implicit external redirect!? The examples, however, do not show an "implicit external redirect". Although this particular wording is removed from the 2.2 and 2.4 docs.

aakk9999

5:38 pm on Mar 31, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Why does it say "implicit external redirect" (emphasis my own)?

I am reading this differently - that the word "implicit" has been used because they want to say that it will do external redirect even though there is no [R] specified (with [R] is explicitly said you want a redirect).

As soon as there is a word "external", to me it means it needs to do a round trip to user agent with the response code 301/302.

Otherwise it is not external redirect, it is an internal rewrite.

So if the Apache is not behaving this way then:

- either there is a bug in the documentation
- or there is a bug in the Apache implementation
- or the identification of identical "scheme & host" is not the way we think it is

I cannot think of something fourth.

penders

5:49 pm on Mar 31, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I am reading this differently - that the word "implicit" is because they want to say that it will do external redirect even though there is no [R] specified.


Well, I think I'm reading it the same as you (but was hypothesizing based on all the other examples and statements to the contrary). But if you read it like this it then conflicts with the examples that follow (which don't show an external redirect) and also with the following sentence which they have applied emphasis to... "Remember An unconditional external redirect to your own server will not work...".

I also wonder what they mean by "unconditional"? Why should it matter whether it's conditional or not?

[edited by: penders at 5:56 pm (utc) on Mar 31, 2015]

penders

5:56 pm on Mar 31, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I cannot think of something fourth.


I wondered if this functionality was perhaps dependent on some other directive being set?!

or the identification of identical "scheme & host" is not the way we think it is


Yeah, I wondered this as well, but I've tried accessing the site with the same host as stated in the ServerName directive (ie. the bare domain). And rewriting to the same, but it still redirects. Both Windows and Linux servers.

lucy24

7:38 pm on Mar 31, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



:: detour to test site, followed by experimenting with MAMP config file locally (I don't, of course, have a live server to play with) ::

Well, that was educational: I've now learned what exact directive causes MAMP to ask for my administrator password on startup and shutdown. (Still haven't figured out why, though.) But other than that, no joy. I think I did all the same experimenting you did, and arrived at the identical conclusion: in this specific area, if in none other, Apache's docs are delirious.

:(

:: wandering off to post query in docs discussion area, where questions do on rare occasions get answered ::