Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k & phranque

Message Too Old, No Replies

mod_rewrite: external vs. local redirects

why do my rules only work with the [R] flag?



2:48 am on Jun 5, 2002 (gmt 0)

10+ Year Member

Hi folks,

I just discovered this site for the first time in a Sherlock search, and it looks fantastic. I've been trying to solve a problem for quite a while, and I think I might have found the group of people who can help! (I'm posting in this forum because it seemed to have the greatest number of mod_rewrite posts, but moderators, feel free to move it to a better spot.)

Here's the deal. I have some simple but very useful rewrite rules on several of my CGI-driven sites, including:

RewriteRule ^dynamic/(.+).html /scripts/content.cgi?page=$1\&%{QUERY_STRING}

This takes URLs like /dynamic/home.html and converts them to /scripts/content.cgi?page=home. The "apparent" URLs are easy to for visitors to remember or share, and easy for search engines to index. This works just swell on several Mac OS X servers that I manage.

However, the same rewrite rule doesn't want to work on the RedHat Linux servers that our preferred ISP manages. On those servers, the rewrite rule shown above results in a browser error, "The attempt to load http:/[smilesopper]/www.typea.net/dynamic/home.html failed." I have to use this rule instead:

RewriteRule ^dynamic/(.+).html http:/[smilesopper]/www.typea.net/scripts/content.cgi?page=$1\&%{QUERY_STRING} [R]

This still converts the /dynamic/home.html URL, and brings some of the same benefits as the first setup, but since it's an external redirect (the [R] flag), it changes the visible URL in the web browser's location bar when the page loads. So it's not quite as user-friendly.

My servers and the ISP's servers are both running Apache 3.20, and I have access to the .htaccess files on both. But apparently something else in the server configuration (httpd.conf or elsewhere) is different. The ISP would gladly make the needed changes for me, but they can't figure out what to do.

Can anyone help? What server settings, or installed modules, would allow external redirects to work but cause local redirects to fail? Is there anything I can ask my ISP to look at for clues?

By the way, I've tested some simplified rules to make sure that my regular expressions are correct, etc. The following two rules do not work on the ISP's servers:

RewriteRule ^dynamic2/foo.html greetings.swf 
RewriteRule ^dynamic3/foo.html http:/[smilesopper]/www.typea.net/greetings.swf

but this works:

RewriteRule ^dynamic4/foo.html http:/[smilesopper]/www.typea.net/greetings.swf [R]

You can try these three URLs to see the results of the above three rewrite rules:


Thanks much,


10:15 am on Jun 5, 2002 (gmt 0)

10+ Year Member

Since it is a CGI, you need to use the "hack" passthrough flag in your rule. I.e.

RewriteRule ^dynamic/(.+).html /scripts/content.cgi?page=$1 [PT]

From the manual, it is necessary to use this when the final URL needs to pass through Alias, ScriptAlias, Redirect, etc handlers.


2:58 pm on Jun 5, 2002 (gmt 0)

10+ Year Member

Hi Scotty,

Thanks for the suggestion. Unfortunately, I tried changing my third example rule to

RewriteRule ^dynamic4/foo.html greetings.swf [PT]

but I still received the same error. We're not using script aliases (we're running CGI's directly from a physical directory), so I don't think we need this flag; our OS X servers are working fine without it.

Any other ideas?



3:36 pm on Jun 5, 2002 (gmt 0)

10+ Year Member

Here's one more bit of information that might be useful. The link to [typea.net...] which triggers an external redirect, shows up in the Apache access log as two requests -- one to /dynamic4/foo.html, and a second one to /greetings.swf, with the first request as the referrer. That's what I'd expect...

...but the links to [typea.net...] and [typea.net...] which don't use the [R] flag, don't show up in the access log at all. Apache is choking on those requests before it gets to the point of writing to the log.


5:03 pm on Jun 6, 2002 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member


Two ideas... I'd recommend investigating RewriteBase to see if that has any effect. And also, you might want to use RewriteLog to see if the log gives any hint as to where the internal redirects are going wrong.

Good luck!


5:52 pm on Jun 6, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member

There's nothing obviously wrong with your dynamic2 example, other than that using a relative path name in the replacement string may be ambiguous.

The fully qualified URL in dynamic3 can't work in an internal rewrite, unless you have a subdirectory named "http:" ;)

Btw: In both cases, the error message that Mozilla gives me is "The document contains no data."

It is also a good idea to use the [L] flag on most rules, which will eliminate the danger of several rules jumping in successively. The fact that you don't see those requests in your logs could hint at a recursive application of some rewrite rules, that is then automatically cut off at a certain level without serving anything to the client.

The suggestions about RewriteBase and RewriteLog are probably the way to go, as especially the latter will tell you what really happens.


4:36 am on Jun 7, 2002 (gmt 0)

10+ Year Member


Thanks for your careful read on this. After prodding my ISP a bit more (I've suggested setting up a RewriteLog, but they haven't done that for me yet), I made an interesting discovery. If I browse to either the dynamic2 or the dynamic3 URL through our secure certificate, the rewriting works! Try this:


(The SSL certificate on this server has expired, so you can just click through that error message.)

Our ISP uses a load-balancing system for http requests, but I think https requests are handled by a single server. So it looks like the question is, what exactly about a load-balancing setup would prevent internal redirects from working? And more importantly, is there a way to get around this limitation?

I agree that RewriteBase might be the missing piece -- its documentation might at least hold some answers -- but the description of Apache's internal processing loses me every time:


I've experimented with different settings for RewriteBase, like putting the physical path to my web directory, but haven't had luck with that yet.

So ... if anyone can suggest how I might use RewriteBase -- or explain the internal processing of a redirect, in simple enough terms for at least an experienced sysadmin at my ISP to understand -- I'd be much obliged!

(Otherwise, I will definitely get on them about that RewriteLog.)

See ya,


4:56 am on Jun 7, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member

Apache 3.20

Are they that high?


3:46 pm on Jun 7, 2002 (gmt 0)

10+ Year Member

Sorry -- 1.3.20!


5:21 am on Jun 8, 2002 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member


Since this thread has gone quiet, I assume there aren't any mod_rewrite "power users" available to answer your question. Personally, I just got into the Apache mod_rewrite documentation, and hacked my directives until they worked.

Your situation does sound like some sort of server config problem, since your rewrite rules work on your secure server but not on your http server.

About all I can think of is to have your ISP compare the server config files between the servers that work and those that don't.

Good luck,



Featured Threads

Hot Threads This Week

Hot Threads This Month