Forum Moderators: phranque

Message Too Old, No Replies

RewriteCond with -d doesn't find protected directory

         

Kerms

9:58 pm on May 24, 2008 (gmt 0)

10+ Year Member



Hello,
I have the following dir tree on my site

/
/public_html/
/public_html/.htaccess
/public_html/index.php
/public_html/images/
/public_html/private/
/public_html/private/.htaccess

The .htaccess in the root directory has the following:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

As such when I go to mysite.com/images it goes directly to the folder

However when I go to mysite.com/private or mysite.com/private/
it redirects to my index.php file...

When I remove the .htacess file from the /private directory, everything works fine. So somehow, the root .htaccess is not seeing the /private as a directory.

Any ideas on how to correct this?

-Kerms

jdMorgan

11:26 pm on May 24, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Strange...

Is /private inaccessible via HTTP? -- In other words, do you have HTTP access denied on that subdirectory?

Jim

Kerms

11:40 pm on May 24, 2008 (gmt 0)

10+ Year Member



Its accessible.

The problem seems like the -d directive is looking at /private and maybe seeing that it is protected... and is considering it to not be a directory?

the .htacess file in the private directory looks like this:

AuthType Basic
AuthName "Private Dir"
AuthUserFile "/home1/mysite/.htpasswds/public_html/private/passwd"
require valid-user

By itself this works... but when I added the .htacess file to the root I was no longer able to get into the /private/ directory because of the redirect.

But when I removed/renamed the .htacess file inside the /private/ folder it worked.... so something about nested .htacess files is causing it to ignore the -d directive and continute with the rewrite rule.

:(

[edited by: Kerms at 11:41 pm (utc) on May 24, 2008]

jdMorgan

2:46 am on May 25, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, either as a test, or as a work-around (mostly 'cause I have no other ideas), how about explicitly bypassing the rule if the request is for /private/ files?

Add:


RewriteCond $1 !^private/

to your rule, and see what happens...

The only time I've ever had problems with -f/-d was when the %{REQUEST_FILENAME} was to be later rewritten. Therefore, -f couldn't find it, because the URL resolved to a non-existent file, since the URL hadn't been rewritten yet.

Jim

Kerms

4:30 am on May 25, 2008 (gmt 0)

10+ Year Member



Thanks for the tip, but I gave that a try and no luck. It still doesn't work with that condition.

The requested dir DOES exist, but somehow the existance of the additional .htacess file inside it is messing things up. It also has something to do with the fact that it is requring the authentication because if I just put a .htacess file that doesn't require authenticantion, everything works fine.

Appreciate any and all ideas.

jdMorgan

1:31 pm on May 25, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Perhaps RewriteOptions Inherit is not set by default on your server... Again, just a guess -- but worth checking.

Jim

Kerms

11:15 pm on May 25, 2008 (gmt 0)

10+ Year Member



THanks for all your replies. I finally found a good way to trace requests and here is what was happenning.

In the root directory of my site, I have both index.php and my .htaccess file...

THe rewrite condition was seeing my request to my private directory as a query to my index.php

I added

RewriteCond %{QUERY_STRING} ^/private$

And it resolved my issues.

Really appreciate all your input Jim!

[edited by: Kerms at 11:16 pm (utc) on May 25, 2008]

Kerms

4:17 am on May 26, 2008 (gmt 0)

10+ Year Member



Ok, I obvious made a silly error. Adding the above line compeltely broke my friendly url functionality.
Still looking for ideas.

jdMorgan

5:30 am on May 26, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



How about:

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

Jim

Kerms

2:05 am on May 27, 2008 (gmt 0)

10+ Year Member



Wow after all these didn't work (which i just tried all variations again, I finally added debugging on the output page and looked at why it was going there.

Turns out when it goes to /private it sees a 401 Error, Unauthorized access. Since I didn't have a default document setup for that error page so it when to index.php/401.shtml which got parsed by the rule and was accepted as not a valid file or directory!

Googled on that and I found: [andrewrollins.com...]

Added ErrorDocument 401 default to this and it resolved the issues all together!

I really appreciate your help as I don't think I would have gotten this far without your help!

jdMorgan

1:07 pm on May 27, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Glad you found the problem/solution! -- I didn't think about the ErrorDocument at all!

Posting the solution here will doubtless help others in the future, though, so thanks for letting us know.

Jim