2010-04-19 83 views
1

是否可以通過.htaccess保護單個URL?例如,我將有website.com/index.php?skin=name。我可以密碼只保護這個網址嗎? (沒有PHP更改,只有.htaccess.htaccess代碼保護單個網址?

website.com/index.phpwebsite.com/index.php?skin=other_name不應受限制。

回答

4

您可以重寫地址來保護它是這樣的:

RewriteEngine On 
RewriteCond %{REQUEST_URI} ^/index\.php$ 
RewriteCond %{QUERY_STRING} ^skin=(name)$ 
RewriteRule ^(.*)$ /skin/%1 [PT] 

<LocationMatch "/skin/name"> 
    AuthType Basic 
    AuthName "By Invitation Only" 
    AuthUserFile /path/to/passwords 
    Require valid-user 
</LocationMatch> 
+0

您不能在.htaccess文件中使用LocationMatch – 2016-11-08 15:10:03

0

你最好打賭就是用PHP來做。快速谷歌搜索後,我發現使用PHP_AUTH一個偉大的方式,看起來很像的.htaccess登錄提示

<?php 
$login_successful = false; 

// check user & pwd: 
if (isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['PHP_AUTH_PW'])){ 

    $usr = $_SERVER['PHP_AUTH_USER']; 
    $pwd = $_SERVER['PHP_AUTH_PW']; 

    if ($usr == 'jonas' && $pwd == 'secret'){ 
     $login_successful = true; 
    } 
} 

// login ok? 
if (!$login_successful){ 

    // send 401 headers: 
    // realm="something" will be shown in the login box 
    header('WWW-Authenticate: Basic realm="Secret page"'); 
    header('HTTP/1.0 401 Unauthorized'); 
    print "Login failed!\n"; 

} 
else { 
    // show secret page: 
    print 'you reached the secret page!'; 
} 

來源:http://snippets.dzone.com/posts/show/2006