Go Back   CodingForums.com > :: Server side development > Apache configuration

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-10-2011, 06:27 PM   PM User | #1
klgiger
New Coder

 
Join Date: Mar 2011
Posts: 12
Thanks: 3
Thanked 0 Times in 0 Posts
klgiger is an unknown quantity at this point
looking to use htaccess to call script based on file name and directroy

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.
klgiger is offline   Reply With Quote
Old 04-10-2011, 06:36 PM   PM User | #2
oesxyl
Master Coder


 
Join Date: Dec 2007
Posts: 6,682
Thanks: 436
Thanked 890 Times in 879 Posts
oesxyl is a jewel in the roughoesxyl is a jewel in the roughoesxyl is a jewel in the rough
Quote:
Originally Posted by klgiger View Post
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:
Code:
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
oesxyl is offline   Reply With Quote
Users who have thanked oesxyl for this post:
klgiger (04-10-2011)
Old 04-10-2011, 06:44 PM   PM User | #3
klgiger
New Coder

 
Join Date: Mar 2011
Posts: 12
Thanks: 3
Thanked 0 Times in 0 Posts
klgiger is an unknown quantity at this point
Thanks for your help I tried that but doesn't redirect.

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^([^\/]+)\/(.+)$ count.php?id1=$2&id2=$1
klgiger is offline   Reply With Quote
Old 04-10-2011, 06:52 PM   PM User | #4
oesxyl
Master Coder


 
Join Date: Dec 2007
Posts: 6,682
Thanks: 436
Thanked 890 Times in 879 Posts
oesxyl is a jewel in the roughoesxyl is a jewel in the roughoesxyl is a jewel in the rough
Quote:
Originally Posted by klgiger View Post
Thanks for your help I tried that but doesn't redirect.

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^([^\/]+)\/(.+)$ count.php?id1=$2&id2=$1
this?:
Code:
RewriteRule ^(.+)$ count.php
best regards
oesxyl is offline   Reply With Quote
Users who have thanked oesxyl for this post:
klgiger (04-10-2011)
Old 04-10-2011, 07:01 PM   PM User | #5
klgiger
New Coder

 
Join Date: Mar 2011
Posts: 12
Thanks: 3
Thanked 0 Times in 0 Posts
klgiger is an unknown quantity at this point
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.
klgiger is offline   Reply With Quote
Old 04-10-2011, 07:04 PM   PM User | #6
oesxyl
Master Coder


 
Join Date: Dec 2007
Posts: 6,682
Thanks: 436
Thanked 890 Times in 879 Posts
oesxyl is a jewel in the roughoesxyl is a jewel in the roughoesxyl is a jewel in the rough
Quote:
Originally Posted by klgiger View Post
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

Edit: 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
oesxyl is offline   Reply With Quote
Users who have thanked oesxyl for this post:
klgiger (04-10-2011)
Old 04-10-2011, 07:16 PM   PM User | #7
klgiger
New Coder

 
Join Date: Mar 2011
Posts: 12
Thanks: 3
Thanked 0 Times in 0 Posts
klgiger is an unknown quantity at this point
Awesome Oesxyl your ROCK that worked like a charm. Thanks for your time.
klgiger is offline   Reply With Quote
Old 04-10-2011, 07:31 PM   PM User | #8
klgiger
New Coder

 
Join Date: Mar 2011
Posts: 12
Thanks: 3
Thanked 0 Times in 0 Posts
klgiger is an unknown quantity at this point
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!
klgiger is offline   Reply With Quote
Old 04-10-2011, 07:43 PM   PM User | #9
oesxyl
Master Coder


 
Join Date: Dec 2007
Posts: 6,682
Thanks: 436
Thanked 890 Times in 879 Posts
oesxyl is a jewel in the roughoesxyl is a jewel in the roughoesxyl is a jewel in the rough
Quote:
Originally Posted by klgiger View Post
Awesome Oesxyl your ROCK that worked like a charm. Thanks for your time.
you are welcome,

Quote:
Originally Posted by klgiger View Post
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:
PHP Code:
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
oesxyl is offline   Reply With Quote
Old 04-11-2011, 05:12 AM   PM User | #10
klgiger
New Coder

 
Join Date: Mar 2011
Posts: 12
Thanks: 3
Thanked 0 Times in 0 Posts
klgiger is an unknown quantity at this point
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 is offline   Reply With Quote
Old 04-11-2011, 06:49 AM   PM User | #11
klgiger
New Coder

 
Join Date: Mar 2011
Posts: 12
Thanks: 3
Thanked 0 Times in 0 Posts
klgiger is an unknown quantity at this point
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]
klgiger is offline   Reply With Quote
Old 04-11-2011, 10:02 AM   PM User | #12
oesxyl
Master Coder


 
Join Date: Dec 2007
Posts: 6,682
Thanks: 436
Thanked 890 Times in 879 Posts
oesxyl is a jewel in the roughoesxyl is a jewel in the roughoesxyl is a jewel in the rough
Quote:
Originally Posted by klgiger View Post
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...ml#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
oesxyl is offline   Reply With Quote
Old 04-11-2011, 01:17 PM   PM User | #13
klgiger
New Coder

 
Join Date: Mar 2011
Posts: 12
Thanks: 3
Thanked 0 Times in 0 Posts
klgiger is an unknown quantity at this point
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.
klgiger is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:36 PM.


Advertisement
Log in to turn off these ads.