Project

General

Profile

mod_rewrite issues with $HTTP["referer"] block

Added by imk over 16 years ago

What I want to happen is that if someone directly links to a specific set of zip files, it will take them to the main page for that file.

I also have a bunch of regular rewrites that I want to work no matter how the user made it to the site.

.
.
.

Here's how it should work:

# The user clicked on the zip file from within my domain, let it through
$> curl -I "http://www.domain.com/some/page/file_1.zip" 
HTTP/1.1 200 OK
# The file is downloaded successfully
# The user clicked on a hotlink to the .zip file from another site, redirect them to the main page
$> curl -e "http://www.otherdomain.com" -I "http://www.domain.com/some/page/file_1.zip" 
HTTP/1.1 200 OK
# The user ends up seeing http://www.domain.com/some/page/ instead of downloading the file

.
.
.

This is my rewrite stuff:

url.rewrite-once = (
    "^/path/that/doesnt/exist/index.html$" => "/path/that/does/exist/index.html" 
)

$HTTP["referer"] !~ "^($|http://(www\.)?domain\.com)" {
    url.rewrite-once = (
        "^/some/page/file_(.*)\.zip$" => "/some/page/" 
    )
}

.
.
.

The rule works for the zip file, but it affects the rest of the rules.

# The user tries to access the index.html directly
$> curl -I "http://www.domain.com/path/that/doesnt/exist/index.html" 
HTTP/1.1 200 OK
# /path/that/does/exist/index.html is loaded successfully
# The user tries to access the same file with a referer specified
$> curl -e "http://www.otherdomain.com" -I "http://www.domain.com/path/that/doesnt/exist/index.html" 
HTTP/1.1 404 Not Found
# It returns a 404 instead of actually displaying /path/that/does/exist/index.html

.
.
.

Even though the rewrite is not part of the $HTTP["referer"] check, it still seems to be affected by it. If I comment out the $HTTP["referer"] stuff, then the rewrites return 200 for both.

.
.
.

This is what I see is happening:

  • The user clicks on a link from my site
  • It skips the referer check because it's direct
  • It then looks at the url.rewrite-once rule below it
  • It downloads the file or loads the page in the second example
  • The user clicks on a link from another site
  • It matches the referer (since it's not coming from *domain.com)
  • It's then stuck inside of the $HTTP["referer"] block
  • It never makes it to the url.rewrite-once below the block
  • It will load the page instead of download the zip
  • It returns a 404 for anything else that was rewritten below since it never makes it to that block

.
.
.

Is this the intended behavior?

It looks like I would need to duplicate my rules -- put one inside of the referer block, and then outside of the block.

Thanks in advance. :)


Replies (2)

RE: mod_rewrite issues with $HTTP["referer"] block - Added by icy over 16 years ago

Try url.rewrite-once += in the referer block

    (1-2/2)