0

So, if I have the following URL:

http://www.example.com/Foo/Bar/Drink/

The last node of the URL (in this case, Drink) will be either a directory, in which case I want to serve index.php, or it will be a file named "Drink.php", in which case I want to serve them that file.

http://www.example.com/ would obviously serve index.php.

Both would maintain that "pretty URL" look with the trailing slash.

Is this possible? The site will follow this format consistently and I'm really trying to avoid having to route everything through the main index file.

Tealstone
  • 30
  • 2

2 Answers2

1

place this code in .htaccess under the root directory of your site

Options -MultiViews
RewriteEngine ON    

RewriteCond %{REQUEST_FILENAME} -d
RewriteRule (.+) /$1/index.php [QSA,L]

RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ /$1.php [QSA,L]
Amine Hajyoussef
  • 4,381
  • 3
  • 22
  • 26
  • This is dynamic, so it's pulling the index.php each time it's actually a directory, but when it's a file, it's give a 403 forbidden, like it's trying to get into a directory within an index file. – Tealstone Jun 21 '13 at 20:22
  • Seems like there were some issues because our institution mounts some of these web folders over our SAN... Also appears as though the first rewrite condition and rule weren't needed since index would be served anyways. I added another rule into there to add the trailing slash. Here's the final product. Thanks, Amine! Options -MultiViews RewriteEngine ON # add trailing slash RewriteCond %{REQUEST_URI} !(/$|\.) RewriteRule (.*) %{REQUEST_URI}/ [R=301,L] #if it's a file, serve the file. RewriteCond %{DOCUMENT_ROOT}/$1.php -f RewriteRule ^(.+?)/?$ /$1.php [QSA,L] – Tealstone Jun 21 '13 at 20:46
  • Grr.. Apparently requires at least 15 rep. I'm a new user at 10. If I get to 15, I'll come back and upvote ya. :) – Tealstone Jun 22 '13 at 00:52
0

I think a rewrite rule might do the trick. Try something like this:

Options +FollowSymLinks
DirectoryIndex index.php

RewriteEngine On
RewriteBase /Foo/Bar/Drink
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /Foo/Bar/Drink HTTP/
RewriteRule ^Drink.php$ http://www.yourdomain.com/Foo/Bar/Drink [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /Foo/Bar/Drink/Drink.php [L]
Benjamin Stark
  • 440
  • 3
  • 14