Why isn't this rewrite rule working?

Discussion in 'Apache' started by Colbyt, Nov 12, 2009.

  1. #1
    I thought I understood mod-rewrite after reading the sticky but I guess I don't.

    I have a script configured where it writes the friendly URLs to the page in this format as the entries are read from the DB:
    /interger/The-title-of-the-item.html where interger is the uniue id # of the row.
    sample:
    /1/The-title-of-the-item.html

    I want to rewite
    /1/The-title-of-the-item.html
    to
    view.php?id=1

    so when the link (/1/The-title-of-the-item.html) is clicked the page view.php?id=1 is loaded and the browser displays Domain.com/1/The-title-of-the-item.html

    I certainly thought that:

    Options +FollowSymLinks
    Options +Indexes
    RewriteEngine On
    RewriteBase /
    RewriteRule ^/([0-9]+)/(.*)\.html$ /view.php?id=$1 [L]
    PHP:
    would work but it does not seem to.

    Or did I miss the boat completely?
     
    Colbyt, Nov 12, 2009 IP
  2. chadsmith

    chadsmith Peon

    Messages:
    82
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Try using
    RewriteRule ^([0-9]+)/(.*)\.html$ /view.php?id=$1 [L]
    Code (markup):
     
    chadsmith, Nov 12, 2009 IP
    Colbyt likes this.
  3. Colbyt

    Colbyt Notable Member

    Messages:
    3,224
    Likes Received:
    185
    Best Answers:
    0
    Trophy Points:
    210
    #3
    That did not work either. I tried it again even though I thought I had already.

    At least we agree that the syntax is close to right.

    Any other takers?
     
    Colbyt, Nov 12, 2009 IP
  4. tolra

    tolra Active Member

    Messages:
    515
    Likes Received:
    36
    Best Answers:
    1
    Trophy Points:
    80
    #4
    The rule chadsmith gave is correct, do you have any error messages?

    That rule expects view.php to be in the root of the site, is that the case? if not try removing the / before view.php in the rule.
     
    tolra, Nov 12, 2009 IP
    Colbyt likes this.
  5. Colbyt

    Colbyt Notable Member

    Messages:
    3,224
    Likes Received:
    185
    Best Answers:
    0
    Trophy Points:
    210
    #5
    Guys I completely agree that it should be working but it is not. His rule was just my rule with the / removed. Since I was doing this only localhost I went ahead and uploaded the 3 files to the server.

    It did not work there either. Mod-rewite is enabled both on localhost and on the server.

    Sooner or later I will find some stupid something that is not right and then I will update the thread.

    I will also check the thread in case anyone has an inspiration.
     
    Colbyt, Nov 12, 2009 IP
  6. tolra

    tolra Active Member

    Messages:
    515
    Likes Received:
    36
    Best Answers:
    1
    Trophy Points:
    80
    #6
    Describe not working, is the rule ignored or is there's an error or what?
     
    tolra, Nov 12, 2009 IP
  7. Colbyt

    Colbyt Notable Member

    Messages:
    3,224
    Likes Received:
    185
    Best Answers:
    0
    Trophy Points:
    210
    #7
    The correct URL is written to the browser and the page is not found. Standard browser resource not found.

    If I use view.php?id=X the page loads fine. For testing and debugging I have dual links on the first page so that I can click either.

    So I assume the problem is in transferring the value of the id.

    I may turn on the Apache logs and see if that shows anything worth looking at but I won't do that tonight.
     
    Colbyt, Nov 12, 2009 IP
  8. tolra

    tolra Active Member

    Messages:
    515
    Likes Received:
    36
    Best Answers:
    1
    Trophy Points:
    80
    #8
    In that case, either the server is completely ignoring the rewrite or the path to view.php is wrong, so assuming the server isn't ignoring you, check the path to view.php is correct, try without the leading /.

    Logs should have more detail on what it's looking for and not finding.
     
    tolra, Nov 12, 2009 IP
  9. Colbyt

    Colbyt Notable Member

    Messages:
    3,224
    Likes Received:
    185
    Best Answers:
    0
    Trophy Points:
    210
    #9
    I promised to update this thread when I found the answer. After examing the .htaccess files for scripts that worked and this one that did not, I determined that the problem was in the application of RewriteBase.

    When running a script on localhost using xammpp, where active dierctory is xampp/htdocs/dirname then RewriteBase needs /dirname/ in order for proper operation. Commenting out the line works just fine also.

    I have noted the problem and the solution as remarks in code below. I am sure there is a valid use for RewriteBase but not using it at all seems to be the easier choice.

    Options +Indexes
    Options +FollowSymlinks
    RewriteEngine on
    
    # rewrite base will need to be adjusted for use on server, most likely to / 
    # localhost on xammpp, when active dierctory is xampp/htdocs/dirname then RewriteBase needs /dirname/
    # or just don't use it at all.  It seems to work fine without it also.
    # RewriteBase /news/
    
    RewriteRule ^([0-9]+)/(.*)\.html$ view.php?id=$1 [L,NC]
    
    Code (markup):

    Here's hoping that this thread saves someone else from a fews hours of frustration some time.
     
    Colbyt, Nov 15, 2009 IP
  10. starwebbo

    starwebbo Peon

    Messages:
    119
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #10
    COLBYT
    Good post. I am testing something similar and I am seeking help as it is not working.
    Moving a static site to a dynamic domain -each page individually. May be i can send you a private message for help?
     
    starwebbo, Nov 23, 2009 IP