Link to home
Create AccountLog in
Avatar of ivanhalen
ivanhalenFlag for Italy

asked on

Apache mod_rewrite changes the REAL server port output in a PHP script

I noticed that when using mod_rewrite, PHP returns server_port 80 even if apache is on port 8080.

i.e I have this url: http://myvirtualhost.8080/index.php and this PHP snippet:
<?php echo $_SERVER['SERVER_PORT']; ?>

Open in new window

In .htaccess I have this simple rule
RewriteEngine on
RewriteRule		^/?$			index.php			[NC,L]

Open in new window

With mod_rewrite on, output is "80" (wrong)
With mod_rewrite off, output is "8080" (correct)

Is it normal? Anysimple solutions for this?
The fact is that i need to check the REAL server port to use in a conditional PHP script, something like:
if ($_SERVER['SERVER_PORT'] == 80) {
do something
} else {
do something else

Open in new window

and of course this is actually not possible since with mod_rewrite on the SERVER_PORT is always 80. I prefer not to mess with .htaccess file, so a PHP script solution would be great...

Thanks in advance!
Avatar of gsx1022
Flag of Hungary image

Link to home
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
this is due to redirection. you can redirect the requests to port 8080 as well.

RewriteEngine On
RewriteCond %{THE_REQUEST} ^[a-z]{3,9}\ /login\.html\ HTTP/ [NC]
RewriteRule ^.*login\.html$ [R=301,L]

check example 5 of this page
Avatar of ivanhalen


@gsx1022: thanks, I go checking!
@ddsh79: I'd prefer a solution that doesn't force me to "hardcode" the server name, and something more specific to my need
Thanks, this works perfectly!