views:

86

answers:

4

I've got a Web site with a bunch of docs and pdfs in /foo/bar/

Directory browsing is turned off, so you have to know the specific URL/URI combo to get the document. I'd like to find a way of redirecting ALL requests to /foo/bar/ to a php page that checks their auth cookie is set and, if so, gets their file. Of course, I also need the rule not to trigger if the request is for auth_test.php, in order to prevent looping. I have been fighting with the .htacess in /foo/bar/ and I simply cannot get this to work. mod_rewrite makes regexes look clear and obvious by comparison.

Here is my current .htaccess file (in /foo/bar/):

RewriteEngine On
RewriteRule !^auth_test\.php$ /foo/bar/auth_test\.php?q=$1 [R=301,L]

This successfully works for request to /foo/bar/auth_test.php, but a request to /foo/bar/something sends me to /foo/bar/auth_test.php?q=

It's like $1 isn't even picked up. Additionally, this rule seems to be ignored if the file exists, so requests to /foo/bar/mydocumnent.doc result in the user being prompted to download the requested file. I've even tried to add

RewriteCond %{REQUEST_FILENAME} -f

to prevent this, but that doesn't seem to work. I'm totally at a loss here. Any suggestions?

A: 

In order for $1 to work you need to set a capturing group to be matched and assigned to $1 Try this

RewriteRule !^(auth_test\.php)$ /foo/bar/auth_test\.php?q=$1 [R=301,L]

I am not sure what checks you are doing inside auth_test.php but using RewriteRules and Conditions you can also perform cookie checks inside the .htaccess file.

duckyflip
In order for any of `$n` to work, you first need a match. But there can be no match if you negate the expression.
Gumbo
A: 

@duckyflip

Unfortunately, I had already tried the grouping thing. The contents of $1 is still empty.

ghiotion
A: 

Try either this:

RewriteCond $1 !^auth_test\.php$
RewriteRule (.*) /foo/bar/auth_test.php?q=$1 [R=301,L]

Or this:

RewriteCond %{REQUEST_URI} ^/foo/bar/(.*)
RewriteRule !^auth_test\.php$ /foo/bar/auth_test.php?q=%1 [R=301,L]
Gumbo
That's awesome. It solves the issue of putting the last part of the URI into the GET parameter, but a request to /foo/bar/mydocument.doc still serves up the document (if and only if mydocument.doc exists in /foo/bar/).
andrew
A: 

@Gumbo (sorry, I keep having to switch machines, hence my bouncing around of accounts)

I added in

RewriteCond %{REQUEST_FILENAME} .*doc$|.*pdf$ [NC]

and that seems to prevent .docs and .pdfs from being downloaded. You're the best.

ghiotion
whoops, no it didn't.
ghiotion