Preserving Port Number in RewriteRule

Oh, the strange things mod_rewrite does!

Postby Cerin » Fri Jun 26, 2009 1:32 pm

richardk wrote:Everything else is fine except for the cgi.server_port variable being wrong?

Yeah, and now that you mention it, I'm not sure how that's possible. I have a different site on a different server (IIS) running on port 80, but the site that's running Apache on port 8880 is the one that's showing port 80. I'm starting to suspect if may be a bug in my J2EE app server, which is something called Caucho Resin (not too familiar with it).

richardk wrote:Some things to try:

Put the following in your .htaccess file
Code: Select all
Options +FollowSymLinks

RewriteEngine On

# Get the port from the original request.
RewriteRule ^one$ /two?sp1=%{SERVER_PORT} [QSA,L]
# Get the port after a rewrite.
RewriteRule ^two$ /three?sp2=%{SERVER_PORT} [QSA,L]
# Get the port after another rewrite.
RewriteRule ^three$ /four?sp3=%{SERVER_PORT} [QSA,R,L]

Go to /one and it should redirect to show the ports at each step. This will show if rewriting is the problem.

That shows the correct port.

richardk wrote:What happens if you don't use a <VirtualHost> and just set the server to listen on port 8880? This will show if the problem is Apache/mod_rewrite or the other modules your are using.

Same thing, although I'm currently already only listening on port 8880.

richardk wrote:I also found this thread: Error in CGI.server_port.

That's an interesting link. They basically found a bug in their Java server that was causing the problem. I'll try asking the Resin folks if this may be an issue with their server.
Posts: 6
Joined: Tue Jun 23, 2009 7:19 am

Solution found

Postby transmogribenno » Mon Nov 02, 2009 10:17 pm

I had a very similar problem on a site I was developing, so I decided to set up a local server and test out the one->two->three->four .htaccess file outlined above.

When I attempted to visit

I was redirected to

I found that I could fix the problem in two ways:
1) Remove the ServerName directive completely for that VirtualHost
2) Use a fully specified ServerName directive (including scheme and port number) as per the Apache documentation, i.e.:
Code: Select all
ServerName http://redir-test.localhost:8001

Either of those redirects to the correct location:
http://redir-test.localhost:8001/four?s ... 1&sp1=8001

Posts: 1
Joined: Mon Nov 02, 2009 10:10 pm


Return to Idiosyncrasies

Who is online

Users browsing this forum: No registered users and 1 guest