The Apache RewriteRule documentation 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?
dklynn, I’m sorry, but I can’t make heads or tails of your replies in this thread. I’m not sure you understand the point I’m making in my post.
This is the syntax of a RewriteRule:
RewriteRule Pattern Substitution [flags]
Apache documentation says that if the Substitution is an absolute URL pointing to the current host, it will not perform an external redirect. It says you have to use the [R] flag to perform an external redirect in that situation.
In fact, making Substitution an absolute URL pointing to the current host is resulting in an external redirect when I test it. The rewrite log indicates this as “implicitly forcing redirect”. The [R] flag is not used.
I’m asking what the explanation is for the discrepancy between the documented behavior and the actual behavior.
Your syntax is correct, I can’t argue with the Apache documentation and am at a loss as to why it’s acting the way I think it should act when the official docs say it should work otherwise.
My understanding is that you can use the R=301 to force the redirection to be displayed OR use an absolute (EXTERNAL) redirect to do the same thing (external redirects, i.e., those with http://{yadda-yadda} MUST be displayed so pirates can’t merely use your web pages “on their websites”). Absolute (INTERNAL) redirects tend to confuse Apache as it starts looking for physical path information but is useful when the directory level gets confusing - that will NOT be displayed without the R flag.