1

我们通过单个下载链接分发软件产品的不同版本。交付是基于referer与默认值相结合的,它工作正常。此外,用户应该被重定向到一个404页面,以防使用了错误的文件名。

目前.htaccess-file 看起来像这样:

# stop directory listing
Options -Indexes

# turn rewrite engine on
RewriteEngine On

# force 404 if file name is missing or wrong
RewriteCond %{REQUEST_URI} !^(download_mac\.zip|download_pc\.zip)$
RewriteRule (.*) 404/index.html [L]

# an example based on the referer
RewriteCond %{HTTP_REFERER} ^http://([^.]+\.)*domain-a\.com [OR]
RewriteCond %{HTTP_REFERER} ^http://([^.]+\.)*domain-b\.com
RewriteRule ^(download_mac\.zip|download_pc\.zip)$ domain_ab/$1 [L]

# last rule if no referer matches
RewriteRule ^(download_mac\.zip|download_pc\.zip)$ default/$1 [L]

所以我对这个文件有一个问题和一个附加问题:

  1. 第一条规则,强制 404,非常贪心,每次都得到错误页面,不管调用什么 URL。我还尝试了单个语句,例如RewriteCond %{REQUEST_URI} !^download_mac\.zip$没有任何效果。我怎样才能解决这个问题?
  2. 如何摆脱任何其他规则中的文件名?我尝试过类似的事情,RewriteRule ^(.*)$ default/$1 [L]但这让我很难过,而且500 Internal Server Error.
4

2 回答 2

1

您可以通过使用这样的 Env 变量来避免重复文件名:

RewriteRule ^(download_mac\.zip|download_pc\.zip)$ - [E=ALLOWED:$1,NC]

RewriteCond %{ENV:ALLOWED} ^$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ /404/index.html [L]

RewriteCond %{ENV:ALLOWED} !^$
RewriteCond %{HTTP_REFERER} ^http://([^.]+\.)*domain-a\.com [OR]
RewriteCond %{HTTP_REFERER} ^http://([^.]+\.)*domain-b\.com
RewriteRule ^ /domain_ab/%{ENV:ALLOWED} [L]

RewriteCond %{ENV:ALLOWED} !^$
RewriteRule ^ /default/%{ENV:ALLOWED} [L]
于 2013-04-22T16:07:59.083 回答
1

您可以将重写规则移到最后。其他规则处理有效案例,如果它们都不匹配,则应用最后一条规则

# an example based on the referer
RewriteCond %{HTTP_REFERER} ^http://([^.]+\.)*domain-[ab]\.com
RewriteRule ^download_(mac|pc)\.zip$ domain_ab/$0 [L]

# last rule if no referer matches
RewriteRule ^download_(mac|pc)\.zip$ default/$0 [L]

# force 404 if file name is missing or wrong
RewriteRule ^ 404/index.html [L]
于 2013-04-22T16:33:37.897 回答