...

View Full Version : looking to use htaccess to call script based on file name and directroy



klgiger
04-10-2011, 06:27 PM
I was trying to figure out how to call a script but redirect to the file
based on file name and directory. So dir and file could always be different.

What I what I want to do is be able to count mp4 file requests.

So if it is /adfjwk3/file.mp4

Then I would still redirect to the mp4 but also call count.php?id1=FileName&id2=Directory

Is this even possible? Or if someone has a better idea for counting direct downloads that would be great. And no I can use php to deliver the file because
it gets played in flash player or downloaded to mobile devices. I need to keep the direct download and be able to count it.

oesxyl
04-10-2011, 06:36 PM
I was trying to figure out how to call a script but redirect to the file
based on file name and directory. So dir and file could always be different.

What I what I want to do is be able to count mp4 file requests.

So if it is /adfjwk3/file.mp4

Then I would still redirect to the mp4 but also call count.php?id1=FileName&id2=Directory

Is this even possible? Or if someone has a better idea for counting direct downloads that would be great. And no I can use php to deliver the file because
it gets played in flash player or downloaded to mobile devices. I need to keep the direct download and be able to count it.
try:


RewriteRule ^([^\/]+)\/(.+)$ count.php?id1=$2&id2=$1

or you can simple use a rule to redirect all requests to count.php and use $_SERVER['REQUEST_URI'] to find filename and directory.

best regards

klgiger
04-10-2011, 06:44 PM
Thanks for your help I tried that but doesn't redirect.

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^([^\/]+)\/(.+)$ count.php?id1=$2&id2=$1

oesxyl
04-10-2011, 06:52 PM
Thanks for your help I tried that but doesn't redirect.

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^([^\/]+)\/(.+)$ count.php?id1=$2&id2=$1
this?:


RewriteRule ^(.+)$ count.php


best regards

klgiger
04-10-2011, 07:01 PM
Ok great that worked great!!

The only issue is when I try to redirect to another domain I get

The page isn't redirecting properly

Firefox has detected that the server is redirecting the request for this address in a way that will never complete.

oesxyl
04-10-2011, 07:04 PM
Ok great that worked great!!

The only issue is when I try to redirect to another domain I get

The page isn't redirecting properly

Firefox has detected that the server is redirecting the request for this address in a way that will never complete.
yes, is my fault, :)
RewriteRule ^(.+)$ urltodomain [R=301,L]

L stand for last, R is only to return a 301 to the browser

you can try to replace the regex step by step to get both parameters if you want. Probably the regex from my previous was the problem
best regards

klgiger
04-10-2011, 07:16 PM
Awesome Oesxyl your ROCK that worked like a charm. Thanks for your time.

klgiger
04-10-2011, 07:31 PM
Though now I can't actually redirect to that file from the file. I guess what I will need to do is use if it comes from the count.php file redirect to file. I will google that I appreciate what you did!

oesxyl
04-10-2011, 07:43 PM
Awesome Oesxyl your ROCK that worked like a charm. Thanks for your time.
you are welcome, :)


Though now I can't actually redirect to that file from the file. I guess what I will need to do is use if it comes from the count.php file redirect to file. I will google that I appreciate what you did!
is a php problem, i guess you can try something like this in count.php:


print $_SERVER['REQUEST_URI'];

see what you get, and then you could split this in pieces to identify the directory and the file and maybe to tweek the regex in htacces. Probably you need to update a database to count the files and if you need to store other information about request.

best regards

klgiger
04-11-2011, 05:12 AM
Yeah no matter what I try it never picks the file that referred it. In
php it only shows the php page itself as the referrer.

klgiger
04-11-2011, 06:49 AM
Yeah when I got this all working it just goes in a loop.
So it requests the file.mp4 then it redirects to the count.php grabs the file path
I sent with the htaccess file. But since I still need that mp4 file to be displayed
when I use header('Location: x.x.x' ); in the php file to send the browser back
to that file for playing it gets sent right back to count.php and when I check the
db count on that it is raised by a hundred in few seconds so I can see it just
goes in circle.

What I need to say is that if the referer is http://domain.com/dir/count.php goto mp4
file otherwise redirect to the count.php file.

This is what I am trying but not working. I also tried redirecting the mp4 file with a query
string attached like http://domain.com/dir/joe.mp4?p=1 and then redirecting based on
the query string but no luck on that.

RewriteCond %{QUERY_STRING} ^p=1
RewriteRule /* {REQUEST_FILENAME} [R,L]
RewriteRule ^(.+)$ http://domain.com/count.php?id1=%{REQUEST_FILENAME}&id2=$1 [R=301,L]

oesxyl
04-11-2011, 10:02 AM
Yeah when I got this all working it just goes in a loop.
So it requests the file.mp4 then it redirects to the count.php grabs the file path
I sent with the htaccess file. But since I still need that mp4 file to be displayed
when I use header('Location: x.x.x' ); in the php file to send the browser back
to that file for playing it gets sent right back to count.php and when I check the
db count on that it is raised by a hundred in few seconds so I can see it just
goes in circle.

What I need to say is that if the referer is http://domain.com/dir/count.php goto mp4
file otherwise redirect to the count.php file.

This is what I am trying but not working. I also tried redirecting the mp4 file with a query
string attached like http://domain.com/dir/joe.mp4?p=1 and then redirecting based on
the query string but no luck on that.

RewriteCond %{QUERY_STRING} ^p=1
RewriteRule /* {REQUEST_FILENAME} [R,L]
RewriteRule ^(.+)$ http://domain.com/count.php?id1=%{REQUEST_FILENAME}&id2=$1 [R=301,L]
second rewrite rule is never executed because match first one. To avoid redirection from count.php to count.php you can use RewriteCond before the rule( no idea why this is happend since you said you want to redirect to another domain).

RewriteCond %{REQUEST_FILENAME} !^count.php$

http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewritecond

If you want to pass some aditionals parameters use the QSA flag.

http://httpd.apache.org/docs/2.2/rewrite/flags.html

best regards

klgiger
04-11-2011, 01:17 PM
I wasn't actually redirecting to another domain but the path was ../../../file and that didn't seem to work so treating it like a different domain I could use the url.

But the p=1 doesn't match on the first one because when the file is requested it doesn't
have the p=1 I just have the file.mp4. I add that in the redirect back to the file so that I could try to get it to play. I tried to tell it if the referrer is count.php is the referer go ahead and play the file but that didn't work. because really if they view source and see the file with the query string on it and then call that from the browser that didn't count it.
And if they can do that it will allow them free bandwidth because the file counts is what I plan to use to monitor the users bandwidth.

Maybe I just don't understand htaccess because I don't see how my redirects actually
say allow the file to play because everytime the file is requested it has a rule saying to
go to count.php so how does it know to allow me in once its been to count.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum