Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    New Coder
    Join Date
    Mar 2011
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    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.

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    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

  • Users who have thanked oesxyl for this post:

    klgiger (04-10-2011)

  • #3
    New Coder
    Join Date
    Mar 2011
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thanks for your help I tried that but doesn't redirect.

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

  • #4
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    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

  • Users who have thanked oesxyl for this post:

    klgiger (04-10-2011)

  • #5
    New Coder
    Join Date
    Mar 2011
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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.

  • #6
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    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

  • Users who have thanked oesxyl for this post:

    klgiger (04-10-2011)

  • #7
    New Coder
    Join Date
    Mar 2011
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Awesome Oesxyl your ROCK that worked like a charm. Thanks for your time.

  • #8
    New Coder
    Join Date
    Mar 2011
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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!

  • #9
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    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

  • #10
    New Coder
    Join Date
    Mar 2011
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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.

  • #11
    New Coder
    Join Date
    Mar 2011
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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]

  • #12
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    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

  • #13
    New Coder
    Join Date
    Mar 2011
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •