Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.htaccess Rewrite - Fails if file exists with the same name as parameter

I have been having a little trouble with a rewrite rule which, after Googling around led me nowhere. I have a standard rewrite of a URL setup like this:

RewriteRule ^services/(.*)/ /services/index.php?content=$1 [L]

The site uses the value of content to request the related content from the DB but just makes the URL look prettier; standard stuff. This works to a certain extent. If, for example, my URL us /services/testimonials/ the rule works fine; the content gets loaded and the page displays normally. What I was seeing with some URLs though (e.g. /services/training/) was an error stating:

Warning: Unknown: failed to open stream: No such file or directory in Unknown on line 0 Fatal error: Unknown: Failed opening required 'redirect:/services/index.php' (include_path='[removed]') in Unknown on line 0

After much Googling and head banging (with little progress from either) I discovered that if I changed the URL to /services/Training/ (notice the capitalised T) it worked. This quickly led me to a thought about a conflict in the URL with something (which I probably should have thought of first in hindsight). After checking which URLs worked and which didn't, I connected the dots and found the URLs that were broken had files with the same name in subdirectories the same as the URL. That sentence is confusing. Basically I cannot use the URL /services/training/ as there is a file /services/training.php which causes the return of the error.

There is an obvious fix to this which is to rename the files that conflict with the URLs, but is there a way in the rewrite rule where I can get it to ignore any files it may find of the same name?

like image 371
Matt Asbury Avatar asked Jan 17 '12 09:01

Matt Asbury


1 Answers

Options -MultiViews

This should prevent the url from mapping to training.php. It will however not prevent /services/training.php from not mapping, but that won't be a problem I presume.

like image 168
Gerben Avatar answered Sep 22 '22 06:09

Gerben