MOD REWRITE works for .html but not .php

Discussion in 'Apache' started by kzak, Apr 24, 2007.

  1. #1
    using:

    RewriteRule ^(.+)\.html$ foo.php?qs=$1
    Code (markup):
    If I enter example.html in the browser,
    $qs in foo.php is echoed "example"

    But if I change the rule to:

    RewriteRule ^(.+)\.php$ foo.php?qs=$1
    Code (markup):
    when I enter example.php in the browser,
    $qs in foo.php is echoed "foo"

    What is going on? Why isn't $qs in the second example echoing "example"?
     
    kzak, Apr 24, 2007 IP
  2. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #2
    Because your killing the original script, which is foo.php aka (.+).php so it gives up and spits out foo.
     
    Nintendo, Apr 24, 2007 IP
  3. kzak

    kzak Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    is there a way to not kill it?
     
    kzak, Apr 24, 2007 IP
  4. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #4
    Different extention (Dot html is much better SEO wise.), or adding a fake directory.
     
    Nintendo, Apr 24, 2007 IP
  5. exponent

    exponent Peon

    Messages:
    1,243
    Likes Received:
    60
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Nintendo is right, you're script is going fubar on itself.
     
    exponent, Apr 24, 2007 IP
  6. rodney88

    rodney88 Guest

    Messages:
    480
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #6
    You can put your code inside the httpd.conf file and use the [L] flag - this will stop the rewrite processing as soon as it's done the first rewrite (X.php to foo.php). As .htaccess files must be interpreted for each request and operate on a per-directory basis, [L] doesn't work quite as expected when used in .htaccess.

    In that case, you can just add in a rewritecond that checks if it's already on the foo.php script -
    RewriteCond %{SCRIPT_FILENAME} !foo\.php$
    RewriteRule ^(.+)\.php$ foo.php?qs=$1
    Code (markup):
    It depends on how your php scripts work but if they ever need to use the query string, you will want to use the QSA flag otherwise the query string will be overwritten with your qs=filename string.
     
    rodney88, Apr 25, 2007 IP