Preserving Port Number in RewriteRule

Oh, the strange things mod_rewrite does!

Preserving Port Number in RewriteRule

Postby Cerin » Tue Jun 23, 2009 7:28 am

I have several development sites hosted locally, but on different ports. e.g. localhost:8881, localhost:8882, etc

For each site, I have a number of RewriteRules of the form:
Code: Select all
RewriteRule ^some/pretty/path$ /template.php [QSA,NC,L]


I've found that this seems to drop the port number. So I tried changing it to:
Code: Select all
RewriteRule ^some/pretty/path$ http://%{HTTP_HOST}/template.php [QSA,NC,L]


This preserves the port number, but now it causes a redirect, so the URL looks like /template.php, which I don't want.

How do you preserve the port number without causing a redirect? Any help is appreciated.

Regards,
Chris
Cerin
 
Posts: 6
Joined: Tue Jun 23, 2009 7:19 am

Postby richardk » Tue Jun 23, 2009 12:14 pm

What is your set up like?
Are you using one instance of Apache?
Are you using <VirtualHost>s? What are they like?
Is the mod_rewrite in a .htaccess file?
What are the document roots like?

You could do it without different ports: FAQ: Testing many websites on your development computer/server.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby Cerin » Tue Jun 23, 2009 4:04 pm

richardk wrote:What is your set up like?
Are you using one instance of Apache?
Are you using <VirtualHost>s? What are they like?
Is the mod_rewrite in a .htaccess file?
What are the document roots like?

You could do it without different ports: FAQ: Testing many websites on your development computer/server.


I don't see what any of those questions have to do with mod_rewrite, but ok... My setup is one instance of Apache, with a <VirtualHost> for each site. Each site has their own .htaccess file. It's all quite simple.

I'd prefer to figure out *why* mod_rewrite is randomly dropping the port number. Perhaps it's a bug?
Cerin
 
Posts: 6
Joined: Tue Jun 23, 2009 7:19 am

Postby richardk » Wed Jun 24, 2009 8:36 am

I don't see what any of those questions have to do with mod_rewrite, but ok..

So i can test?

One more question Apache 1.3, 2.0 or 2.2? (I will test 1.3 and 2.2 as i have them already.)
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby richardk » Wed Jun 24, 2009 9:33 am

Apache 1.3?

Try removing the
Code: Select all
Port 80

line from your httpd.conf file.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby Cerin » Thu Jun 25, 2009 11:30 am

I'm using Apache2.2.

I also have no "Port 80" in my httpd.conf
Cerin
 
Posts: 6
Joined: Tue Jun 23, 2009 7:19 am

Postby Cerin » Thu Jun 25, 2009 11:40 am

This is only custom change to my httpd.conf:

Code: Select all
Listen 8880

NameVirtualHost localhost:8880
<VirtualHost localhost:8880>
   ServerAdmin developers@domain.com
   DocumentRoot "/usr/share/webroot"
   ServerName localhost.com.local
   ErrorLog "/usr/share/error_log.txt"
   CustomLog "/usr/share/access_log.txt" combined
   
   DirectoryIndex index.cfm index.php index.htm index.html index.html.var
   ResinConfigServer localhost 6800
   <Location /caucho-status>
      SetHandler caucho-status
   </Location>
   <directory "/usr/share/webroot">
      Options All
      AddOutputFilter Includes html
      AllowOverride All
      Order allow,deny
      Allow from all
   </Directory>
</VirtualHost>


And my .htaccess
Code: Select all
RewriteEngine on
RewriteBase   /
RewriteRule   ^prefix/(.*)/(.*)$            /template.cfm?path=$1/$2      [QSA,NC,L]
Cerin
 
Posts: 6
Joined: Tue Jun 23, 2009 7:19 am

Postby richardk » Thu Jun 25, 2009 1:19 pm

I cannot replicate your problem on Apache 2.2.

Are you absolutely certain it is loading the wrong file? What if you make a totally uniquely named text file and rewrite to it?

Try
Code: Select all
Listen 8880

<VirtualHost*:8880>
   ServerAdmin developers@domain.com
   DocumentRoot "/usr/share/webroot"
   ServerName localhost
   ErrorLog "/usr/share/error_log.txt"
   CustomLog "/usr/share/access_log.txt" combined
   
   DirectoryIndex index.cfm index.php index.htm index.html index.html.var
   ResinConfigServer localhost 6800
   <Location /caucho-status>
      SetHandler caucho-status
   </Location>
   <directory "/usr/share/webroot">
      Options All
      AddOutputFilter Includes html
      AllowOverride All
      Order allow,deny
      Allow from all
   </Directory>
</VirtualHost>
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby Cerin » Thu Jun 25, 2009 1:46 pm

I think you misunderstand me.

If I have a page that goes to http://mydomain:1234/prefix/some/path/, it actually goes to http://mydomain/prefix/some/path/. The URL shows ":1234", but if you dump the cgi.server_port variable,you'll see it says 80, which is *wrong*.

If I change the rule to:
Code: Select all
RewriteEngine on
RewriteBase   /
RewriteRule   ^prefix/(.*)/(.*)$            http://%{HTTP_HOST}/template.cfm?path=$1/$2      [QSA,NC,L]


Then it correctly preserves the port, but now it performs a 301 redirect and goes to /template.cfm?path=some/path

It's like whatever proxy-method the relative path uses, uses port 80 when it should be using port 1234, but I don't know how to specify this in the rule.
Cerin
 
Posts: 6
Joined: Tue Jun 23, 2009 7:19 am

Postby richardk » Fri Jun 26, 2009 9:44 am

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

It's like whatever proxy-method the relative path uses, uses port 80 when it should be using port 1234, but I don't know how to specify this in the rule.

For some information about mod_rewrite processing see Apache mod_rewrite Technical Details. Especially API Phases point 2.

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.

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.

I also found this thread: Error in CGI.server_port.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Next

Return to Idiosyncrasies

Who is online

Users browsing this forum: Google [Bot] and 1 guest

cron