Sign up ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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

My Goal is to redirect the request for:




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 * connected
* Connected to localhost ( 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: - - [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 - - [09/Nov/2010:16:55:27 --0500] [localhost/sid#137cfb0][rid#160fc90/initial] (3) applying pattern '(.*)' to uri '/Work/dbUI/pageElements/Dialog/Contact.php' - - [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' - - [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 - - [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 - - [09/Nov/2010:16:55:27 --0500] [localhost/sid#137cfb0][rid#160fc90/initial] (1) escaping http://localhost/Work/dbUI/pageElements/Dialog for redirect - - [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] - - [09/Nov/2010:16:55:27 --0500] [localhost/sid#137cfb0][rid#160fc90/initial] (2) init rewrite engine with requested uri /Work/dbUI/pageElements/Dialog - - [09/Nov/2010:16:55:27 --0500] [localhost/sid#137cfb0][rid#160fc90/initial] (3) applying pattern '(.*)' to uri '/Work/dbUI/pageElements/Dialog' - - [09/Nov/2010:16:55:27 --0500] [localhost/sid#137cfb0][rid#160fc90/initial] (1) pass through /Work/dbUI/pageElements/Dialog
share|improve this question

3 Answers 3

up vote 2 down vote accepted

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

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

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.

share|improve this answer

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]
share|improve this answer
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 :) –  KeatsKelleher Nov 10 '10 at 15:34

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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