views:

53

answers:

2

I have a directory named dollars that contains a file index.php. I would like for the url http://localhost/dollars/foo to translate to dollars/index.php?dollars=foo. This is the .htaccess file that I currently have in the dollars directorty:

Options +FollowSymLinks
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !^index\.php
RewriteRule ^(.+)$ index.php?dollars=$1 [L]

The idea being that any request other than a request to index.php should use the RewriteRule.

However, this does not work.

I've been looking for a while trying to figure out how to create the redirect I want, but I don't even know if I'm on the right track. Regex were never my thing. Thanks for any help!

+1  A: 

A often-used solution for rewrites is to just check that the path being requested doesn't point to an actual file/directory, and rewrite it if it doesn't - since the rewritten URL will then point to an actual file, no loop occurs.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Amber
This is exactly what I need. Perfect! Thank you so much.However, I'm a bit confused about the following problem, though it doesn't really affect me. The url localhost/dollars/index redirects to index.php, without passing 'index' as the $1 parameter. Why is this?Thanks again!
apeace
+1  A: 

Amber's answer should get things working for you, but I wanted to address what was going wrong in your specific case. You had the right idea, but %{REQUEST_FILENAME} actually ends up being a fully qualified path here, so your regular expression should check for index.php at the end, not the beginning.

Consequently, you should find that this will work more like you expect:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !index\.php$
RewriteRule ^(.+)$ index.php?dollars=$1

Swapping out the RewriteConds for those that Amber mentioned would be less problematic if you added other things to that directory, though, so I'd recommend using that in place of this anyway.

Tim Stone
Thank you so much! I see what I was doing wrong. Though, I prefer Amber's solution because it's a bit cleaner. Also, the question I asked Amber also applies to this solution, for reasons I don't understand. Thoughts?
apeace
You must have MultiViews turned on for that directory, I think.Try adding `Options -MutliViews` to that `.htaccess` file and see if it behaves correctly.
Tim Stone
Totally right dude. Someday I'll read a book about this...Thanks again!
apeace
Awesome, glad it's working now!
Tim Stone