Redirect for files matching a regular expression

New to mod_rewrite? This is a good place to start.

Redirect for files matching a regular expression

Postby jb2009 » Mon Jun 15, 2009 10:23 am

Hi,

I'm having one heck of a time trying to figure this out and would love any help that you can offer.

Basically, I need to use mod_rewrite to create a redirect for files beginning with "upl-". On top of that, I need users to have access to these files if a specific cookie is set.

I need this done in mod_rewrite for a number of reasons.

SUMMARY OF WHAT I'M TRYING TO DO:
If requested file name starts with "upl-" and cookie "x" has not been set, redirect to noaccess.php. If the cookie has been set, show the file.

Can anyone help me figure this out? I'm a beginner to mod_rewrite and am completely stumped. I'd be glad to help you with any PHP-related questions as I don't want to come off as a freeloader.

Thanks,
Jon
jb2009
 
Posts: 4
Joined: Mon Jun 15, 2009 10:16 am

Postby richardk » Mon Jun 15, 2009 12:08 pm

%{HTTP_COOKIE} contains all the cookies seperated by "; ", eg.
Code: Select all
one=two; three=four; five=six


The RewriteRule (in a .htaccess files) matches the part of the URL after the directory that the .htaccess file is in and up to the query string. For example if the .htaccess file is at /abc/.htaccess and the request is for /abc/def?ghi=jkl then the RewriteRule would match "def". So
Code: Select all
^upl-.+$

will match only upl-* files in the current directory. Is that what you want?

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine on

RewriteCond %{HTTP_COOKIE} ^(.+;\ )?cookie_name=cookie_value(;\ .+)?$ [NC]
RewriteRule ^upl-.+$ /noaccess.php [QSA,L]


If you want the URL to change to /noaccess.php replace QSA with R.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Thanks!

Postby jb2009 » Mon Jun 15, 2009 10:16 pm

I'll give that a try. After much much research and experimentation (and obviously before I had a chance to read your reply) I came up with this solution:

RewriteCond %{REQUEST_URI} ^(.*)/upl-(.*) [NC]
RewriteCond %{HTTP_COOKIE} !(cookie_name)
RewriteRule ^(.*) http://www.mysite.com/noaccess.php [R,L]

Not sure if that's better of worse than your solution but it appears to be doing the trick.

Thanks again for your help!
Jon
jb2009
 
Posts: 4
Joined: Mon Jun 15, 2009 10:16 am

Postby richardk » Tue Jun 16, 2009 10:27 am

Code: Select all
RewriteCond %{HTTP_COOKIE} !(cookie_name)

will match partial cookie names and cookie values.

Code: Select all
RewriteCond %{HTTP_COOKIE} ^(.+;\ )?cookie_name= [NC]

should just match exact (remove the NC flag for case sensitiveness) cookie names and ignore the value.

Code: Select all
RewriteCond %{REQUEST_URI} ^(.*)/upl-(.*) [NC]

Will match any case of upl-, not just lowercase. It will also match those files in any sub directory of the current directory (you didn't say if that is what you wanted or not).

You should be able to combine this condition with the RewriteRule
Code: Select all
RewriteRule ^(.+/)?upl-.+$ http://www.example.com/noaccess.php [R,L]

Add the NC flag for case insensitive.
Remove (.+/)? to only match files in the current directory.

Using
Code: Select all
/upl-(.*)

means it will match "upl-" (nothing after it)
Code: Select all
/upl-.+

will only match upl-something.

Did the mod_rewrite i posted not work?
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby jb2009 » Wed Jun 17, 2009 9:30 am

The reason I couldn't use your code is that I don't know what the cookie value is going to be (it is a random ID), so I wasn't able to work with this line:

RewriteCond %{HTTP_COOKIE} ^(.+;\ )?cookie_name=cookie_value(;\ .+)?$ [NC]
jb2009
 
Posts: 4
Joined: Mon Jun 15, 2009 10:16 am

Postby richardk » Wed Jun 17, 2009 11:23 am

You can use
Code: Select all
RewriteCond %{HTTP_COOKIE} ^(.+;\ )?cookie_name= [NC]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby jb2009 » Wed Jun 17, 2009 3:30 pm

That clears that dilemma up.
Thanks for all your help with this :)
jb2009
 
Posts: 4
Joined: Mon Jun 15, 2009 10:16 am


Return to Beginner's Corner

Who is online

Users browsing this forum: No registered users and 126 guests

cron