SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Mar 2013
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    problem trying to rewrite url - keep getting 404 or 500 server error

    Hi

    I'm having trouble with rewriting a url using mod rewrite - any help/advice would be greatly appreciated as feel I'm going round in circles trying different things ;-s

    The webpage structure in the site I'm working on is there is a finder.php page in the root (ie /pub-finder.php). This then posts 3 variables to the index page in a sub folder called pubs (so /pubs/index.php) this displays a list of results and then on clicking the 'more information' link on the page alongside one of the search results, this should pass the slug/name of the pub to another php file which displays the full details of the pub (ie /pubs/page.php?name=the-new-inn).

    Hope that makes sense so far. The pub-finder.php page is fine and it passes and displays results, the problem I've got is with the pub details page coming up with either a 404 or 500 error, depending on what I try.

    Trying to achieve:
    I'd like the url http://www.mydomain.com/pubs/the-new-inn to go to this actual script at http://www.mydomain.com/pubs/page.php?name=the-new-inn

    I'm sure it's simple, but I can't get it working. My .htaccess file is in the root folder (ie public_html folder) and has the following lines in it:-

    Options +FollowSymLinks
    RewriteEngine on
    RewriteBase /
    RewriteRule ^news/(.*)/?$ /news.php?newsTitle=$1 [L]
    RewriteRule ^events/(.*)/?$ /events.php?eventTitle=$1 [L]
    RewriteRule ^/pubs/(.*)/?$ /pubs/page.php?name=$1 [L]
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule (.*) $1.php [L]

    This currently gives me a server 500 error. My understanding was that the pubs line would recognise the url with '/pubs/' in and the string which comes after that between the / and the subsequent / would get passed into the url where I put the $1 - I'm obviously missing something.

    I look forward to a response and thank you in advance if you can help!

    Many thanks

    Will

  2. #2
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,311
    Mentioned
    19 Post(s)
    Tagged
    1 Thread(s)
    I think you ended up with a couple infinite loops.

    RewriteRule ^/pubs/(.*)/?$ /pubs/page.php?name=$1 [L]

    If you start with a URL like /pubs/the-new-inn, then it will match the pattern ^/pubs/(.*)/?$ and be rewritten to /pubs/page.php?name=the-new-inn. So far so good. The problem is that the new URL /pubs/page.php?name=the-new-inn also matches the pattern, and would be rewritten once again to /pubs/page.php?name=page.php. Then it would be rewritten again. And again. And so on until Apache bails with a server error.

    To avoid this infinite loop, you'll need to add a rewrite condition. For example, you can check that the pattern you capture isn't page.php.

    RewriteCond $1 !=page.php
    RewriteRule ^/pubs/(.*)/?$ /pubs/page.php?name=$1 [L]
    "First make it work. Then make it better."

  3. #3
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,311
    Mentioned
    19 Post(s)
    Tagged
    1 Thread(s)
    Another potential problem is this pattern:

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule (.*) $1.php [L]


    For your known good URLs, this will work OK. /news would rewrite to news.php, then that would be the end of it because subsequent iterations would fail the "not a file" test. But for 404 URLs, this will cause your server to rewrite indefinitely. Let's say I request /not-a-page. It's not a file, so it would be rewritten to /not-a-page.php. But that's also not a file, so it would be rewritten to /not-a-page.php.php, which would be rewritten to /not-a-page.php.php.php, and so on.

    In this case, you probably want a condition that tests if the ".php" suffix will result in a file before you proceed with the rewrite.

    # If this request doesn't map to a file...
    RewriteCond %{REQUEST_FILENAME} !-f

    # *And* if this request .php *does* map to a file...
    RewriteCond %{REQUEST_FILENAME}.php -f

    # Then rewrite
    RewriteRule (.*) $1.php [L]
    "First make it work. Then make it better."

  4. #4
    SitePoint Member
    Join Date
    Mar 2013
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Wink

    Quote Originally Posted by Jeff Mott View Post
    Another potential problem is this pattern:

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule (.*) $1.php [L]


    For your known good URLs, this will work OK. /news would rewrite to news.php, then that would be the end of it because subsequent iterations would fail the "not a file" test. But for 404 URLs, this will cause your server to rewrite indefinitely. Let's say I request /not-a-page. It's not a file, so it would be rewritten to /not-a-page.php. But that's also not a file, so it would be rewritten to /not-a-page.php.php, which would be rewritten to /not-a-page.php.php.php, and so on.

    In this case, you probably want a condition that tests if the ".php" suffix will result in a file before you proceed with the rewrite.

    # If this request doesn't map to a file...
    RewriteCond %{REQUEST_FILENAME} !-f

    # *And* if this request .php *does* map to a file...
    RewriteCond %{REQUEST_FILENAME}.php -f

    # Then rewrite
    RewriteRule (.*) $1.php [L]
    Hi Jeff

    Thank you soooo much for your guidance and advice. That made much more sense and now have a better understanding of the rules. I also added a check to ensure it wasn't index.php too and this now works fine for both of the pages. It is now finally working and the working version looks like:-

    Options +FollowSymLinks
    RewriteEngine on


    RewriteRule ^news/(.*)/?$ /news.php?newsTitle=$1 [L]
    RewriteRule ^events/(.*)/?$ /events.php?eventTitle=$1 [L]


    RewriteCond $1 !=page.php
    RewriteCond $1 !=index.php
    RewriteRule ^pubs/(.*)/?$ /pubs/page.php?name=$1 [L]
    RewriteCond %{REQUEST_FILENAME} !-d

    # If this request doesn't map to a file...
    RewriteCond %{REQUEST_FILENAME} !-f

    # *And* if this request .php *does* map to a file...
    RewriteCond %{REQUEST_FILENAME}.php -f

    # Then rewrite
    RewriteRule (.*) $1.php [L]


    You're a legend and saved me soo much time! Many thanks again!!!! :-)

    Will


Tags for this Thread

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
  •