How many users will have capabilities to use modrewrite

Discussion in 'Apache' started by Kalyse, Apr 20, 2007.

  1. #1
    I am coding something and I cannot be bothered to go through the whole of the website again and do a if(modrewrite enabled) thing to display the version depending if its enabled.


    Maybe when I have a spare 6 hours I will, but I was curious, how many servers fo you think dont have it enabled.

    I only ask because every server I have ever worked on has had it enabled, but thats because Ive configured them this way.

    I havent ever rented from anyone for obvious reasons I dont need to.
     
    Kalyse, Apr 20, 2007 IP
  2. rodney88

    rodney88 Guest

    Messages:
    480
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #2
    If you're planning on distributing your script, you'd be very wrong to assume that every server is Apache, let alone with a particular module enabled.

    Checking the config every time there is a URL that you need to output is going to be very tedious. Have you considered using output buffering and then just a single regex search/replace on all the internal links?
     
    rodney88, Apr 20, 2007 IP
  3. Kalyse

    Kalyse Peon

    Messages:
    1,221
    Likes Received:
    24
    Best Answers:
    0
    Trophy Points:
    0
    #3
    No I havent, could you explain the principle for me.

    Many thanks.
     
    Kalyse, Apr 20, 2007 IP
  4. rodney88

    rodney88 Guest

    Messages:
    480
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Assuming PHP, example:
    <?php
    // At top of script before any output, start buffering
    ob_start('prettyURLs');
    
    // Define our callback function (prettyURLs) to replace links
    function prettyURLs($buffer) {
      global $cfg;
      if ( ! $cfg->useModRewrite )
        $buffer = preg_replace('/a href="\/([^"])"/','a href="index.php?req=\\1',$buffer);
      return $buffer;
    }
    
    // Do all your normal stuff
    echo '<a href="/pretty/url/using/mod-rewrite.html">Some link</a>';
    
    // Send the output buffer
    // See manual but basically everything that has previously
    // been echo/printed gets passed to our callback function
    ob_end_flush();
    
    ?>
    PHP:
    The manual explains it better than I can, and the regex will probably require a bit more work. Basically you assume either all links are the "pretty URLs" or they're not, and you print them in that format. Then using the ob_ functions, you stop the all output from echo/print being sent straight to the browser until the end of the script (or until you call a flush() function to send it). You can specify a custom callback function that receives the buffer as a variable, and manipulate the output however you want. In this case, you want to use a regular expression to replace your internal links depending on whether or not you want to use mod_rewrite.
     
    rodney88, Apr 20, 2007 IP