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.
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!
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-innalso 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.
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]
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!!!