mod_rewrite issue. =(

Discussion in 'Apache' started by DLWood, Oct 18, 2006.

  1. #1
    I created the rule below and it seems to have some issues.

    RewriteRule ^([a-z]+)\.html$ index.html?page=$1

    I thought that this rule would take 'anylowercasetext.html' and reform it as 'index.html?page=anylowercasetext' but for some reason the regular expression seems to be acting weird; as in it will allow me to put any text into the URL and then match it to 'index.html?page=index'. (not sure why it puts 'index' for $1)

    Now I'm not sure how this may relate to the problem but if I change the '.html' part of the regular expression to say '.foo'.. the regular expression seems to work 100% correctly.

    So, I am assuming file extensions are somehow the problem.

    Any help would be great -Dan
     
    DLWood, Oct 18, 2006 IP
  2. pulsa

    pulsa Peon

    Messages:
    66
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I think this is what that rule will do:
    Put 'anylowercasetext.html' into the URL ---> content of 'index.html' will show up.

    Uses the code below for the same results ( with '.foo' )
    
    AddType application/x-httpd-html .foo
    
    RewriteEngine on
    RewriteRule ^([a-z]+)\.foo$ index.html?page=$1 [NC]
    
    Code (markup):
    Is this what you was trying to do?
     
    pulsa, Oct 18, 2006 IP
  3. DLWood

    DLWood Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Naw, I am trying to get

    'anylowercasetext.html' -> index.html?page=anylowercasetext

    But for some reason the text "html" in the regular expression is messing things up. ie: ^([a-z]+)\.html$

    If I change that text to something else, like "htm", it works fine.
     
    DLWood, Oct 18, 2006 IP
  4. owned

    owned Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Does your server really process .HTML files with php, cgi or whatever scriptengine. If you're doing this with php you should probably need:

    .PHP ext.
    RewriteRule ^([a-z]+)\.html$ index.PHP?page=$1
    Code (markup):
    (maybe this rule is correct..)

    When user browser sits at this url -> localhost/asdasd.html
    Then index.php would get it like -> index.php?page=asdasd
     
    owned, Oct 19, 2006 IP
  5. DLWood

    DLWood Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Ok so I tested that RewriteRule out and it some what works.

    RewriteRule ^([a-z]+)\.html$ index.PHP?page=$1 [L]

    The problem now is that the regular expression is matching things it shouldn't, say a user types anything like:

    www.domain.com/blah
    www.domain.com/blah.xyz
    www.domain.com/blah.htmlllll
    www.domain.com/2234.html

    These are all examples invalid matches that are triggering a match. The good news is that when a user types something in the correct format, everything works fine.

    Also, when it is matched and the match is a error, the $1 back reference is translated into the word "missing".

    So it translates literally into "index.php?page=missing

    The behaviour that I was looking for was that if it didn't match they would get a 404 error.. which I can catch with another rule and toss to a custom page.


    hmm, any suggestions?
     
    DLWood, Oct 21, 2006 IP