2011-11-08 53 views
0

我試圖限制訪問基於引用的mod_rewrite的網頁。 該網頁的網址是 http://www.example.com/path/to/page.php 它位於Debian服務器上/var/www/path/to/page.php 我有一個包含URL的重定向基於引用使用mod_rewrite忽略GET參數

allowedReferers

http://www.example.com/test/test1.php:white

http://www.example.com/test/test2.php:white

列表的重寫地圖 allowedReferers

我也有以下重寫條件/規則

重寫

Cond %{HTTP_REFERER} ^(.*)$ 
RewriteCond ${allowedReferers:%1|black} ^black$ [NC] 
RewriteRule /* http://www.someotherplace.com [R,L] 

到目前爲止,這工作得很好。

http://www.example.com/test/test1.php

http://www.example.com/test/test2.php

可以訪問該網站,而

http://www.example.com/test/test3.php

被重定向到someotherplace.co米

我的問題是,在現實生活中,我的引用將包含GET參數。 例如

http://www.example.com/test/test1.php?id=245

我的想法是第一條件改寫爲這樣的事情

RewriteCond %{HTTP_REFERER} ^(.*)\?.*$ 

或本

RewriteCond %{HTTP_REFERER} ^(.*)\?id=[0-9]*$ 

我已經在Firefox測試了正則表達式」 RegexTester和他們的行爲,我希望他們。 應用於以下輸入

http://www.example.com/test/test1.php?id=245

他們返回本作$1

http://www.example.com/test/test1.php

我預計%1還包含URL減去GET參數。 這樣,離開規則的其餘部分不變:

RewriteCond ${allowedReferers:%1|black} ^black$ [NC] 
RewriteRule /* http://www.someotherplace.com [R,L] 

應導致預期的行爲:

http://www.example.com/test/test1.php?id=234

http://www.example.com/test/test2.php?id=222

可以訪問該網站,而

http://www.example.com/test/test3.php?id=256

(或http://www.athirdplace.com/等)

將被重定向到someotherplace.com

不幸的是作爲對所有預期它不表現。 突然間,所有引用者都可以訪問該網站,並將其應用於第一種情況。

,因爲我想看看實際上是%1裏面有什麼,我想出了以下的想法:

RewriteCond %{HTTP_REFERER} ^(.*)\?id=[0-9]*$ 
RewriteCond ${allowedReferers:%1|black} ^black$ [NC] 

RewriteRule /* %1 [R,L] 

假設指的頁面從

http://www.example.com/test/test2.php?id=234

會將我重定向到

http://www.example.com/test/test2.php

錯誤的假設。它重定向我

http://www.example.com/var/www/path/to/

是,正如我在開頭提到的,其訪問頁面的地址是受到限制。 當然會引發404,因爲/var/www/是文檔根。

重定向到%1只是拼命嘗試調試我的問題,所以我不需要一個解決方案來實現這一點。我正在尋找的是解決我原來的重定向問題的一種方法。 查閱情況一樣,這些

http://www.example.com/test/test1.php?id=234

http://www.example.com/test/test2.php?id=222

(無論哪個ID傳遞)

http://www.example.com/path/to/page.php

而一切去

http://www.someotherplace.com

最後我也希望任何想法如何調試mod_rewrite,特別的方式來窺視的東西一樣%{HTTP_REFERER}%1$1和喜歡。

回答

0

只要找到了解決(至少部分地)調試的mod_rewrite如何:

http://www.latenightpc.com/blog/archives/2007/09/05/a-couple-ways-to-debug-mod_rewrite

提供到輸出的某些值的mod_rewrite的使用非常方便的技巧。 我修改的例子一點點,下面:

RewriteCond %{HTTP_REFERER} ^(.*)\?id=[0-9]*$ 

RewriteRule (.*) /path/to/mod_rewrite_debugger.php?referer=%{HTTP_REFERER}&p1=%1 [R=301,L,QSA] 

mod_rewrite_debugger.php只包含

echo "<pre>"; print_r($_GET); echo "</pre>" 

輸出是:

[referer] => http://vfh143.beuth-hochschule.de/tests/moodle3.php?id=3 

[p1] => http://vfh143.beuth-hochschule.de/tests/moodle3.php 

這表明,我原來的設想是對。

不幸的是,調試器不會再施加第二條件時,工作:

RewriteCond %{HTTP_REFERER} ^(.*)\?id=[0-9]*$ 

RewriteCond ${allowedReferers:%1|black} ^black$ [NC] 

RewriteRule (.*) /path/to/mod_rewrite_debugger.php?referer=%{HTTP_REFERER}&p1=%1 [R=301,L,QSA] 

產生以下輸出:

[referer] => http://vfh143.beuth-hochschule.de/tests/moodle3.php?id=3 

[p1] => 

原來的規則突然擔任原本打算。問題本身就解決了。

也許這可以幫助別人。