homepage Welcome to WebmasterWorld Guest from 54.197.215.146
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
htaccess help - how to redirect /password/files.all to /files.all?
vaxop




msg:1507758
 6:02 am on May 18, 2003 (gmt 0)

This question is driving me nuts.. ive been working at it for two days now

ive tried
RewriteRule ^/password(.*)$ /$1 [R,L]
RewriteRule ^/password(.*)$ $1 [R,L]
RewriteRule ^password(.*)$ /$1 [R,L]
RewriteRule ^password(.*)$ $1 [R,L]

yet none of them work.. what am i doing wrong?

----------------------------------------
Heres what im trying to accomplish
Im making an anti-hotlink script, but more advanced and with more features than a simple .htaccess referer check. whenever a user requests a file (like jpg, mpg, gif, etc), my current htaccess will redirect them to /r.php?file.ext, by using:

rewriterule (.*)\.mpg$ /r.php?a=$1.mpg [R,L]
rewriterule (.*)\.jpg$ /r.php?a=$1.jpg [R,L]

in r.php, the algorithm checks to see if the user can download the file, and if they can, a header is sent ie "Location: file.jpg". However, heres where the problem arises. when the php file sends the header to acknowledge downloading of the file, the htaccess file catches the request, and sends it back to the php script, and the php back to the htaccess file creating an infinite loop (at least thats what i think is happening). the php file sends a request to show file.jpg, but when it does that, .htaccess kicks in and redirects the request back to r.php?a=file.jpg, and the circle continues.

I thought about a workaround to this, and the only thing i could come up with would be for the php script to send a request to a nonexistant folder with the file name, in this case "/password". I was hoping that the htaccess file would catch any requests to /password/file.ext, and allow all files requested from that folder to properly download, making the final htaccess file to look like

rewriterule ^/password(.*)$ /$1 [R,L]
rewriterule (.*)\.mpg$ /r.php?a=$1.mpg [R,L]
rewriterule (.*)\.jpg$ /r.php?a=$1.jpg [R,L]

with L telling the server to stop looking any further down the list. since i cant get /password/files.all to redirect to files.all, i dont know if ill run into the infinite loop problem again - whether after the request is sent to show any file in /password/, it goes back into the htaccess file to come across line 2 or 3, and start the infinite loop once more...

Please, please someone help me with this bugger.. am i on the right track? how in gods name can i redirect a request for a file to a php script, and then allow the user to download the file if the php script acknowledges, without going into the infinite loop?

 

vaxop




msg:1507759
 7:56 am on May 18, 2003 (gmt 0)

RedirectMatch password/(.*) /$1 [R,L]

works to redirect.. but it still creates the infinite loop..

ARRGHHH

how can i workaround this?

jdMorgan




msg:1507760
 8:35 pm on May 18, 2003 (gmt 0)

vaxop,

First (titled) question:

RewriteRule ^password/(.*)$ /$1 [L]

should work.

You can simplify the problem and also help with the infinite loop problem by using server-internal redirects, rather than the 302 external redirects ( [R,L] defaults to [R=302,L] ). Also, note that this rule combines your two rules into one:

RewriteRule ^(.*\.[mj]pg)$ /r.php?a=$1 [L]

The advantage is that if you do an internal redirect, .htaccess is not invoked a second time. If your script then outputs a unique directory name (different from the one requested by the browser) then you should not have a recursion problem.

HTH,
Jim

vaxop




msg:1507761
 3:11 am on May 19, 2003 (gmt 0)

no luck..

the htaccess file is
RewriteEngine on
RewriteRule ^(.*\.[mj]pg)$ /s.php [L]

s.php contains
Location: test.jpg

however, when i visit s.php, or test.jpg on the net, they will not show, and i think the loop is going..

when htaccess is deleted, they load perfectly..

any idea what i can do to get this to work?

jatar_k




msg:1507762
 11:57 pm on May 19, 2003 (gmt 0)

Have you taken a look at this vaxop? Some of the resources in that thread may help.

An Introduction to Redirecting URLs on an Apache Server [webmasterworld.com]

jdMorgan




msg:1507763
 12:22 am on May 20, 2003 (gmt 0)

vaxop,

Looks like the PHP "location" directive generates a 302 redirect, and then you're back in the loop. Any external redirect of this type is going to re-run the mod_rewrite code in .htaccess. You must account for this in the rewriterule layout, and also in your file/directory naming conventions to avoid recursion.

I would suggest using a subdirectory for the images, whether it is a real subdirectory or not. If a real subdirectory is used, then a reference to that in your php won't re-invoke the rewriterule if you change it to:

^([^/]*\.[mj]pg)$ /s.php [L]

If you use a fake subdirectory in your php file, then you will need a second rewriterule to redirect from the fake subdirectory to wherever those image actually reside. It too should be a transparent redirect.

^fakesubdir/(.*\.[mj]pg)$ /realpathtoimages/$1 [L]

"realpathtoimages/" can be blank IF this rule is second in order in .htaccess.

Note that these two rules are mutually-exclusive; One requires a subdirectory in the path, and the other requires no subdirectory.

HTH,
Jim

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved