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

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]

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.

[FONT=Courier New]# 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][/FONT]

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!!! :slight_smile:

Will