SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Jul 2004
    Location
    Glasgow, Scotland
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Maintaining SERVER_PORT value through rewrite rules

    I have a pretty standard set of rewrite conditions and rules to translate a "nice url" into the correct file name & query string. The rules work fine.

    e.g.
    Code:
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ([^/]*)$            index.php?section=$1 [L,QSA]
    I also have two instances of apache running to allow me to use the both php4 and php5 modules with apache. One server listens on 80 and the other on 81.

    If the rewrite rule fires, the $_SERVER['SERVER_PORT'] value in PHP will end up as 80, irregardless of the original value.

    I've done some further checks - running the cgi php and both PHP4 and PHP5 on other ports and I figure the the '80' is coming from Apache as part of the rewrite process.

    Is it possible to frame the rewrite rule to ensure the rewritten url maintains the original port?

  2. #2
    SitePoint Member
    Join Date
    Jul 2004
    Location
    Glasgow, Scotland
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am still interested in a rewrite based reply. For now I have noticed that Apache sets a REDIRECT_STATUS value if the url is altered by a rewrite rule. The original port number, if not 80 (note: I've not checked what happens under https), is part of the HTTP_HOST value. In PHP the following code "fixes" the SERVER_PORT value.
    Code:
    function fixServerPort($default=80) {
      if (!empty($_SERVER['REDIRECT_STATUS'])) {
        $start = strpos($_SERVER['HTTP_HOST'],':');
        $port = ($start !== false) ? (int)substr($_SERVER['HTTP_HOST'],$start+1) : $default;
        // correct 'SERVER_PORT' value
        if ($port && $port != $default) {
          $_SERVER['SERVER_PORT'] = $port;
        }
      }
    }
    It works under my circumstances. I can't say how generic it is.

  3. #3
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,605
    Mentioned
    19 Post(s)
    Tagged
    2 Thread(s)
    C_S,

    I would have gone back to the redirection and made it with an absolute link to the correct port, i.e., RewriteRule ([^/]*)$ http://www.example.com:81/index.php?section=$1 [L,QSA]. Of course, I would NOT have used ([^/]*) to capture the filename (strip the path) - because it SHOULD match index.php and loop.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  4. #4
    SitePoint Member
    Join Date
    Jul 2004
    Location
    Glasgow, Scotland
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the reply.

    It doesn't match index.php because of the RewriteCond above. But you're right. Normally I would grab the whole string and parse it internally. This was something quick and dirty to match already existing code - which threw up the issue with SERVER_PORT. Hardcoding the port number isn't a nice option, the idea is to be able to use the same rewrite rules on different ports - enabling me to run different versions of PHP against the same code with a minimum of effort. But ... I guess I should be able to capture the port in a RewriteCond and back reference it in the RewriteRule. Its not ideal as it means I also need to specify the relative path from the document root to the .htaccess file's directory.

  5. #5
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,605
    Mentioned
    19 Post(s)
    Tagged
    2 Thread(s)
    cb,

    Too true about the if file exists but that's a bad habit to get into (IMHO).

    {SERVER_PORT} can be used rather than hardcoding a specific port. I'd think that it would be passed through with the redirection but, if you're having problems, I'd guess that it isn't. Anyway, try the absolute redirect (assuming that you want the URL seen) with http://%{HTTP_HOST}:%{SERVER_PORT}/whatever and see what that does for you.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •