Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rewrite if folder doesn't exist?

I have the following htaccess file:

RewriteEngine On
RewriteRule ^([^/]*)$ /bio.php?bio=$1 [L]

I need it to do the following:

  • Get the following rewritten URL: http://www.website.com/john-smith to go to /bio.php?bio=john-smith (that kind of works at the moment)
  • If there is already a folder (eg /about-us/) then show the file in that instead. At the moment it does but it adds ?bio=about-us on the end.
  • Ideally if possible work with & without the trailing slash.

Any help much appreciated.

Thanks

like image 247
kieran Avatar asked Dec 17 '10 10:12

kieran


1 Answers

Try something like this:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ bio/bio.php?q=$1 [L]

First line will skip the RewriteRule if it finds a matching physical file; second line will skip it if it finds a matching directory. The third line is the rewrite rule that will be executed if the preceding condtions are met.

like image 108
Spudley Avatar answered Sep 21 '22 11:09

Spudley