簡體   English   中英

apache密碼保護特定URL

[英]apache password protect specific URL

我需要對特定URL進行密碼保護,例如任何人都可以訪問index.php,但是一旦他們添加了參數或“ GET”變量,我就必須要求有效用戶。

index.php->甜

index.php?prev = 1->需要有效用戶

有趣的問題。

這是一個解決方案:

<Location /fakeindex.php>
  Order Deny,Allow
  Deny from All
  Allow from env=REDIRECT_STATUS
  AuthType Basic
  AuthUserFile /pathto/htpasswd/file/passwords.secret
  AuthName This is an example auth
  Require valid-user
</Location>
<Directory /path/to/doc/root>
    Order allow,deny
    allow from all
    RewriteEngine On
    RewriteCond %{QUERY_STRING} .
    RewriteCond %{REQUEST_URI} index.php
    RewriteRule .* fakeindex.php [L,QSA]
</Directory>

這部分:

  Order Deny,Allow
  Deny from All
  Allow from env=REDIRECT_STATUS

是否可以防止直接訪問/fakeindex.php(但實際上誰想強制直接訪問受保護的區域)。 這是可選的。

關鍵是這些條件規則檢測到一個非空的查詢字符串,以及對index.php的請求:

    RewriteCond %{QUERY_STRING} .
    RewriteCond %{REQUEST_URI} index.php

如果它們符合規則:

RewriteRule .* fakeindex.php [L,QSA]

在內部將請求重定向到/fakeindex.php,並使用QSA保留查詢字符串。 之后,將應用位置,並且在位置上進行身份驗證。

您可以在get變量上使用isset()方法,例如:

if (isset($_GET['prev'])) {
//load password input form
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM