Jump to content

Recommended Posts

I want to give out the url 'http://www.example.com/tracker/path/to/file.xml', and the mod_rewrite will redirect that request to 'http://www.example.com/get_item.php?fileName=path/to/file.xml'.  get_item.php redirects the user to 'http://www.example.com/path/to/file.xml' with a 302 response, and executes some code.

 

Oh, and I also want the mod_rewrite to strip off any parameters placed in the original url, so 'http://www.example.com/tracker/path/to/file.xml?foo=foobar' does exactly the same thing as if there wasn't '?foo=foobar' at the end.  (This aspect of the coding is not critical, however)

 

My mod_rewrite code looks like this:

 

---

 

Options +FollowSymlinks

RewriteEngine On

RewriteRule ^tracker/([\x00-\x7F]+)/?$ get_item.php?fileName=$1 [L]

 

---

 

However, it acts as if the .htaccess file wasn't even there - I get a 'Page not found' response from the  server.

 

Help would be much appreciated.

 

Note:  I've verified that the PHP code is not the issue - it works fine when the user browses directly to 'http://www.example.com/get_item.php?fileName=path/to/file.xml'.

Link to comment
https://forums.phpfreaks.com/topic/127774-append-file-structure-to-php-argument/
Share on other sites

Just to clear things up, the get_item.php file acts as a hit counter, tracking how many times each request passed to it is downloaded.  Here's that code:

 

<?php

$url = $_GET['fileName'];
$url = substr($url,1);
header('HTTP/1.1 302 Moved Temporarily');
header('Location: '.$url);


//$len = strlen($url);

$counter = "counter/";
//$counter .= str_replace("/",":",substr($url,0,$len-4));
$counter .= str_replace("/",":",$url);
$counter .= ".txt";

$hits = file($counter);
$fp = fopen($counter , "w");

$hits[0] ++;
fputs($fp , "$hits[0]");
fclose($fp);

exit;
?>

 

 

So I've managed to get the mod_rewrite code to this:

 

Options +FollowSymlinks
RewriteEngine On
RewriteRule ^tracker(.*)$ get_item.php?fileName=$1 [L]

 

If the URL is 'http://www.example.com/tracker/file.xml', it'll pass '/file.xml' to the fileName parameter, and I just have the PHP file cut off the leading '/'.  The hit counter registers 17 consecutive hits to the file before the browser calls it quits, as there are too many redirects.

 

If there is a deeper path, such as '/tracker/path/file.xml', it'll register one hit to 'path/file.xml', one hit to 'path/path/file.xml', one hit to 'path/path/path/path/file.xml', etc..., so the number of '/path' levels is doubled every time.  It does this five times before it actually passes it to the server, which unsuprisingly says that the file is not found.

Actually, I figured it out.  As it turns out, the php code was redirecting the user with a relative file path, so the user got sent repeatedly back to '/tracker/path/to/file.xml'.  I fixed it by setting the URL to an absolute path.

 

<?php

$url = $_GET['fileName'];
$url = "http://www.example.com" . $url;
header('HTTP/1.1 302 Moved Temporarily');
header('Location: '.$url);

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.