I'm using mod_rewrite to rewrite GET requests to a particular script.

My Goal is to redirect the request for:

http://localhost/Work/dbUI/pageElements/Dialog/Contact.php?id=138750

to

http://localhost/Work/dbUI/pageElements/Dialog.php?dialog=Contact.php&id=138750

I'm using:

RewriteEngine On
RewriteLog "/var/log/rewrite"
RewriteLogLevel 3
RewriteCond %{THE_REQUEST} /Work/dbUI/pageElements/Dialog/
RewriteCond %{QUERY_STRING} id=([^&?]*)
RewriteRule (.*) http://localhost/Work/dbUI/pageElements/Dialog?dialog=Contact.php&id=%1 [R=302]

It works quite well; but when I make the request I get the following headers (this is the output of a cURL routine):

* About to connect() to localhost port 80 (#0)
*   Trying 127.0.0.1... * connected
* Connected to localhost (127.0.0.1) port 80 (#0)
> GET /Work/dbUI/pageElements/Dialog/Contact.php?id=138750 HTTP/1.1
Host: localhost
Accept: */*

< HTTP/1.1 302 Found
< Date: Tue, 09 Nov 2010 21:37:54 GMT
< Server: Apache/2.2.16 (Fedora)
< Location: http://localhost/Work/dbUI/pageElements/Dialog?dialog=Contact.php&id=138750
< Content-Length: 338
< Connection: close
< Content-Type: text/html; charset=iso-8859-1

As you can see, I get a 302 and a Location header that redirects the client. Is it possible to simply serve the content as opposed to a redirect to the content? The goal is to mask the actual URL for the content.

When I try adding replacing [R=302] with [L] I get the following output in my rewrite log:

127.0.0.1 - - [09/Nov/2010:16:55:27 --0500] [localhost/sid#137cfb0][rid#160fc90/initial] (2) init rewrite engine with requested uri /Work/dbUI/pageElements/Dialog/Contact.php
127.0.0.1 - - [09/Nov/2010:16:55:27 --0500] [localhost/sid#137cfb0][rid#160fc90/initial] (3) applying pattern '(.*)' to uri '/Work/dbUI/pageElements/Dialog/Contact.php'
127.0.0.1 - - [09/Nov/2010:16:55:27 --0500] [localhost/sid#137cfb0][rid#160fc90/initial] (2) rewrite '/Work/dbUI/pageElements/Dialog/Contact.php' -> 'http://localhost/Work/dbUI/pageElements/Dialog?dialog=Contact.php&id=138750'
127.0.0.1 - - [09/Nov/2010:16:55:27 --0500] [localhost/sid#137cfb0][rid#160fc90/initial] (3) split uri=http://localhost/Work/dbUI/pageElements/Dialog?dialog=Contact.php&id=138750 -> uri=http://localhost/Work/dbUI/pageElements/Dialog, args=dialog=Contact.php&id=138750
127.0.0.1 - - [09/Nov/2010:16:55:27 --0500] [localhost/sid#137cfb0][rid#160fc90/initial] (2) implicitly forcing redirect (rc=302) with http://localhost/Work/dbUI/pageElements/Dialog
127.0.0.1 - - [09/Nov/2010:16:55:27 --0500] [localhost/sid#137cfb0][rid#160fc90/initial] (1) escaping http://localhost/Work/dbUI/pageElements/Dialog for redirect
127.0.0.1 - - [09/Nov/2010:16:55:27 --0500] [localhost/sid#137cfb0][rid#160fc90/initial] (1) redirect to http://localhost/Work/dbUI/pageElements/Dialog?dialog=Contact.php&id=138750 [REDIRECT/302]
127.0.0.1 - - [09/Nov/2010:16:55:27 --0500] [localhost/sid#137cfb0][rid#160fc90/initial] (2) init rewrite engine with requested uri /Work/dbUI/pageElements/Dialog
127.0.0.1 - - [09/Nov/2010:16:55:27 --0500] [localhost/sid#137cfb0][rid#160fc90/initial] (3) applying pattern '(.*)' to uri '/Work/dbUI/pageElements/Dialog'
127.0.0.1 - - [09/Nov/2010:16:55:27 --0500] [localhost/sid#137cfb0][rid#160fc90/initial] (1) pass through /Work/dbUI/pageElements/Dialog
link|improve this question
feedback

3 Answers

up vote 2 down vote accepted

Try this: In your RewriteRule, replace the [R=302] flag with [L].

link|improve this answer
didn't work, see edits above for more information – akellehe Nov 9 '10 at 21:57
feedback

If you declare ServerName localhost or ServerAlias localhost, then mod_rewrite would strip off the http://localhost and see that it can simply remap rather than redirect.

link|improve this answer
feedback

OK! Not sure why this works and the other doesn't; but this is the answer. If someone can explain the difference between this and what I had before I'll gladly accept theirs as the correct answer.

RewriteEngine On
RewriteLog "/var/log/rewrite"
RewriteLogLevel 3
RewriteCond %{THE_REQUEST} /Work/dbUI/pageElements/Dialog/
RewriteCond %{QUERY_STRING} id=([^&?]*)
RewriteRule ^/Work/dbUI/pageElements/Dialog/(.*) /Work/dbUI/pageElements/Dialog?dialog=$1&id=%1 [L]
link|improve this answer
1  
Okay, I'll bite: Your original RewriteRule explicitly includes http://localhost, which mod_rewrite takes as a hint to do an external redirect regardless of whether you specify the [R] flag. By removing the http://localhost and the [R] flag from the rule, mod_rewrite no longer feels compelled to do a redirect. – Steven Monday Nov 10 '10 at 15:28
Thanks Steven, I appreciate the information :) – akellehe Nov 10 '10 at 15:34
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.