我在Windows PC中運行apache2和php5。重定向到特定頁面,如果沒有登錄 - .htaccess
我已使用.htaccess
和.htpasswd
保護我的目錄。如果未設置登錄信息,或者用戶名 - 密碼組合不正確,則默認情況下,如果用戶嘗試訪問受保護的目錄,瀏覽器將提示輸入用戶名和密碼框。但我想將用戶重定向到特定的地址或網址。總之,我想重定向用戶而不是顯示HTTP基本認證對話框。我如何使這可能?
在此先感謝...... :)
blasteralfred
我在Windows PC中運行apache2和php5。重定向到特定頁面,如果沒有登錄 - .htaccess
我已使用.htaccess
和.htpasswd
保護我的目錄。如果未設置登錄信息,或者用戶名 - 密碼組合不正確,則默認情況下,如果用戶嘗試訪問受保護的目錄,瀏覽器將提示輸入用戶名和密碼框。但我想將用戶重定向到特定的地址或網址。總之,我想重定向用戶而不是顯示HTTP基本認證對話框。我如何使這可能?
在此先感謝...... :)
blasteralfred
修訂的答案...我相信你可以用mod_rewrite做到這一點。這裏是我發現的一個例子:
# turn on rewrite engine
RewriteEngine on
# if authorization header is empty (non-authenticated client)
RewriteCond %{HTTP:Authorization} ^$
# redirect to new url
RewriteRule /current/path /new/path
警告空洞......我目前無法測試這個。試一試,把它放在你的.htaccess中,並改變路徑以適合你的環境。
我如何使用這個如果用戶沒有登錄? – 2011-04-21 17:28:02
是否有可能? – 2011-04-21 17:36:03
@blasteralfred - 我認爲是這樣的...工作的修訂答案。敬請期待... – 2011-04-21 17:39:25
除Apache(通過.htaccess)外,您還可以使用http authentication in PHP。這可能會給你更多的控制權。
從手冊:
if (!isset($_SERVER['PHP_AUTH_USER'])) {
header('WWW-Authenticate: Basic realm="My Realm"');
header('HTTP/1.0 401 Unauthorized');
echo 'Text to send if user hits Cancel button';
exit;
} else {
// do the redirect here?
}
我想要一個解決方案使用htaccess – 2011-04-23 10:08:12
我在哪裏必須指定正確的用戶名和密碼?@jmathai – Black 2016-04-04 07:11:40
我這類似於AJ的一種方法工作。我.htaccess
文件非常類似以下內容:
AuthUserFile /opt/www/htaccess
AuthType Basic
DirectoryIndex public.txt
<Files "secret.txt">
require valid-user
FileETag None
Header unset ETag
Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
Header set Pragma "no-cache"
Header set Expires "Wed, 11 Jan 1984 05:00:00 GMT"
</Files>
<Files "public.txt">
FileETag None
Header unset ETag
Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
Header set Pragma "no-cache"
Header set Expires "Wed, 11 Jan 1984 05:00:00 GMT"
</Files>
RewriteEngine On
RewriteBase/
RewriteCond %{HTTP:Authorization} !=""
RewriteRule ^$ secret.txt [L]
這樣,網站的行爲如下:
1)訪問的基本URL - >看到public.txt內容。 2)訪問/secret.txt - >提示認證,並顯示secret.txt的內容。 3)再次訪問基本URL - >查看secret.txt中的內容。
使用[L,R]
而不是[L]
將使用302響應來處理重定向。如果您希望重定向在瀏覽器的位置字段中可見,這是一個不錯的選擇。
<擱置>是的,我意識到這是一個非常晚的答案。不過,Google搜索結果中的問題很高,所以我想詳細說明我的方法,以防我發現自己在將來進行相同的搜索。如果其他人受益,則更好。 < /拋開>
我有同樣的問題,雖然這是一個古老的線程,最後我簡單地使用401錯誤文件,如果驗證失敗,顯示特定的網頁...
ErrorDocument 401 /not-logged-in.php
這似乎以簡單的方式爲我做了訣竅。
答案解釋:
你需要實現自定義的認證,本身你不能驗證重定向失敗。
解決方案:
使用HTML meta標籤,使重定向可能在驗證失敗,並且讓用戶訪問身份驗證成功保護區自定義的ErrorDocument實現(服務器總是趕在401輸入用戶名和密碼,因爲瀏覽器是不是擺在首位期待這樣的認證和前第一負載得到拒絕訪問)
AuthUserFile /path/to/users
AuthName "Access Denied"
AuthGroupFile /dev/null
AuthType Basic
Require valid-user
ErrorDocument 401 "<html><meta http-equiv=\"refresh\" content=\"0;url=/failed.html\"></html>"
備選方案I:
自Apache 2.4以來。您可以使用mod_auth_form
與htaccess的,使先進的認證和使用更可靠的解決方案
http://httpd.apache.org/docs/trunk/mod/mod_auth_form.html
替代II:
使用PHP來處理401 ErrorDocument 401 /handle.php
http://php.net/manual/en/features.http-auth.php
擴展安全:
ErrorDocument 401 "<html><meta http-equiv=\"refresh\" content=\"0;url=/kickout.php\"></html>"
ErrorDocument 400 /kickout.php
ErrorDocument 403 /kickout.php
ErrorDocument 500 /kickout.php
Deny from all
Allow from 192.200.x.x
Allow from 192.200.x.x
Allow from 127.0.0.1
Allow from localhost
感謝您使用最少量代碼的解決方案 – 2016-03-10 07:46:09
你是說你想重定向**而不是顯示HTTP Basic Auth對話框,還是你說如果登錄失敗,你想重定向到自定義錯誤文檔? – 2011-04-21 17:17:34
重定向所有用戶,或僅僅是那些沒有登錄的用戶? – 2011-04-21 17:18:11
你在這裏:)。我想重定向而不是顯示HTTP Basic Auth對話框。如果登錄失敗或未設置,我想重定向。 – 2011-04-21 17:19:19