views:

96

answers:

2

I have a site which redirects all requests for files/folders which don't exist to an index file using .htaccess:

RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d 
RewriteRule ^.*$ - [NC,L]
RewriteRule !admin/* index.php [NC,L]

There is a folder "admin/" which has the following in .htaccess for auth:

AuthType Basic
AuthName "admin"
AuthUserFile "/path/to/passwd"
require valid-user

Adding the auth .htaccess file in "admin/" causes the request to be trapped by mod-rewrite instead of providing the authentication response. I've tried a few different things trying to work around this (including this: http://stackoverflow.com/questions/1641666/htaccess-rewrite-and-auth-conflict), but couldn't get any purchase.

Thanks.

EDIT: If I'm already authenticated, the rewrite rule works allowing me to access the "admin/" folder. So it seems that it's the authentication challenge that's doing something wonky.

A: 

when you say you tried the solution from the other post, what did your code look like?

Something like this:

RewriteCond %{REQUEST_URI} !/admin/

I don't see why that wouldn't work.

Matthew J Morrison
I tried exactly that prior to posting this question, and it didn't work. That's when I figured that was more going on than I knew. RewriteCond %{REQUEST_FILENAME} -s [OR] RewriteCond %{REQUEST_FILENAME} -l [OR] RewriteCond %{REQUEST_FILENAME} -d RewriteCond %{REQUEST_URI} !admin/ RewriteRule ^.*$ - [NC,L] RewriteRule ^.*$ index.php [NC,L]
ironkeith
A: 

Let me suggest a simplified form for the rewriting rules:

RewriteCond %{REQUEST_FILENAME} !-s
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule !^admin/ index.php [NC,L]

This incorporates Matthew's suggestion of checking the path against /admin/ (in .htaccess files you omit the leading slash).

You might also need to use a

RewriteBase /

before the first RewriteCond line.

David Zaslavsky
I gave that a try, but unfortunately it didn't work (unless I removed the .htaccess file from the admin folder). Also, removing "RewriteRule ^.*$ - [NC,L]" breaks the rewrite.
ironkeith
How did it break?
David Zaslavsky
It no longer redirected requests to the index.php file. They all just 404'd.
ironkeith
Maybe something is wrong with the pattern `!^admin/`. You could try changing it to `.*` (i.e. match everything) just as a test to see what happens.
David Zaslavsky
Naw, the rule works perfectly, just not when there's a .htaccess auth file in the "admin/" folder. It's really got me baffled.
ironkeith
Hm, well, I'm out of ideas :( sorry...
David Zaslavsky