Forum Moderators: phranque

Message Too Old, No Replies

RewriteRule with absolute URL substitution not performing as expected

         

Jesse_M

6:37 pm on May 20, 2010 (gmt 0)

10+ Year Member



The Apache RewriteRule documentation [httpd.apache.org] says that to perform an external redirect back to the current host, you have to use the [R] flag rather than just using an absolute URL in the substitution. I don't find that to be the case though, with Apache 2.2.4 on CentOS 4.

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.


I have the following in httpd.conf:

RewriteRule A.html http://example.com/B.html [L]


Requesting http://example.com/A.html results in an external redirect and the rewrite log shows the following:

(2) init rewrite engine with requested uri /A.html
(3) applying pattern 'A.html' to uri '/A.html'
(2) rewrite '/A.html' -> 'http://example.com/B.html'
(2) implicitly forcing redirect (rc=302) with http://example.com/B.html
(1) escaping http://example.com/B.html for redirect
(1) redirect to http://example.com/B.html [REDIRECT/302]


The stripping described in the documentation is not happening. Note the "implicitly forcing redirect".


Am I misunderstanding something or making a mistake, is there a bug in this version of Apache, is the documentation just plain wrong? What's the deal?

g1smd

7:16 pm on May 20, 2010 (gmt 0)

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



Nope. You have the default 302 redirect.

Use [R=301,L] to specify a 301 redirect.

However, I have been wondering about this wording for several weeks now. What's the CanonicalName for the server? I think that might play a part in all of this somehow.

Jesse_M

7:31 pm on May 20, 2010 (gmt 0)

10+ Year Member



Sorry, "nope" what?

I just want to know why it's doing an external redirect at all when my reading of the documentation is that it won't.

I almost posted this originally, but didn't want to muddle things: ServerName is example.com.

Where the documentation says "checks to see whether the hostname matches the current host", I think it should specify exactly what it means by "current host", but I assume it means the value of HTTP_HOST. In this case HTTP_HOST and SERVER_NAME should be identical, and the same as the hostname in the RewriteRule.

jdMorgan

10:33 pm on May 20, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



"Nope," the documentation is not wrong -- cryptic, yes, but not wrong.

The correct syntax for use in a config file NOT inside a <Directory> container would be:

RewriteRule ^/A\.html$ /B.html [L]

For use in a <Directory> container, specify a pattern to match the path relative to that container. For example, in a <Directory /> container, omit the leading slash from the rule pattern.

g1smd is asking what your declared ServerName is, because if it does not exactly match the "example.com" you specified in your substitution path, then an external redirect will be performed. For example, if ServerName is www.example.com, and you put example.com in the rule (or vice-versa), then that's not a match, and a redirect will result.

Using the form above avoids the problem entirely.

Jim

Jesse_M

11:09 pm on May 20, 2010 (gmt 0)

10+ Year Member



Thanks for your reply, but you misunderstood what I'm asking.

For what it's worth, I'm *trying* to perform an exernal redirection. But my actual question is why Apache is not behaving as advertised, according to my reading of the documentation.

The documentation says that you can't perform an external redirection to the same host just by making the Substitution an absolute URL.

It says it will turn the absolute URL into a URL-path and do a regular rewrite.

But in fact it appears that it *does* perform an external redirect when I make the Substitution an absolute URL with the same hostname as the current host. And the rewrite log says "implicitly forcing redirect". I want to know why that's happening, because my reading of the documentation is that it shouldn't.

g1smd is asking what your declared ServerName is


I know, I posted it in my reply.