2011-03-09 5 views
5

.htaccess를 사용하여 특정 URL을 암호로 보호하려고합니다. 다른 URL은 동일한 파일을 가리 키지 만 작동이 다릅니다. 이제는 하나의 URL 만 암호로 보호해야합니다. setenvif를 사용하여이 작업을 수행하려고하지만 작동하지 않는 것 같습니다. 나는 아파치 setenv 모듈의 목적이나 사용을 완전히 이해하지 못할 수도있다.조건부를 수행하는 방법 .htaccess 암호 보호

이 내 코드는 무엇

SetEnvIfNoCase Host "topasswordprotect\.domain\.com$" protecturl 
<IfDefine protecturl> 
    Require valid-user 
    AuthName "Please enter your name and password" 
    AuthType Basic 
    AuthUserFile .htpasswd 
</IfDefine> 
+1

관련 질문 : * [조건부로 아파치 env 변수에 따라 HTTP 인증이 필요합니다] (http://stackoverflow.com/q/5845353/195835) * –

답변

3

나는 마침내 그것을 알아 냈습니다. 실제로 mod_rewrite 엔진을 사용해야했습니다. 나는 내가 여분의 파일이나 디렉토리를 만들 필요가없는 해결책을 가지기로 결심했다.

같은 파일을 별도의 디렉토리, 하나의 .htaccess :

# Case we are in the protected area! 
RewriteCond %{REQUEST_FILENAME} index.php [OR] 
RewriteCond %{REQUEST_FILENAME} !-f [OR] 
RewriteCond %{REQUEST_URI} ^(\/){1}$ 
RewriteCond %{REQUEST_FILENAME} !-d [OR] 
RewriteCond %{REQUEST_URI} ^(\/){1}$ 
RewriteCond %{HTTP_HOST} ^(protected)\.mydomain\.com$ 
RewriteRule ^(.*)$ admin.php%{REQUEST_URI} [L] 

#default rewrite 
RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ index.php/$1 [L] 

#only protect access to admin.php which is a symlink to index.php 
<FilesMatch "admin.php"> 
    AuthUserFile .htpasswd 
    AuthName EnterPassword 
    AuthType Basic 
    require valid-user 
</FilesMatch> 
+0

안녕하세요, 여기 파일에 대한 액세스는 passwording입니다. ... com/blogname /으로 물리적으로 존재하지 않지만 htacess 및 permalinks를 사용하여 리디렉션되는 블로그를 비밀번호로 보호 할 수 있습니까? –

+0

모든 것을 다시 써서/file을 확인하고 파일 file.php을 보호하고, 보호 받았다! 정말 잘 작동합니다! – Clooner

0

직접 직접 가능하지 않을 수도 있습니다 작동하지 않는 것입니다하지만 당신은 원래의 디렉토리가 그것으로 연결했다 다른 암호로 보호 된 디렉토리를 가질 수있다? mod_rewrite을 사용하여 일치 요청을 암호로 보호 된 디렉토리로 재지 정할 수 있습니다.

RewriteEngine on 
RewriteCond %{HTTP_HOST} ^www.myhost.tld$ 
RewriteRule ^/foo/(.*)$ /bar/linkeddir/$1 
3

음, 이것은 실제로 mod_rewrite를하지 않고 SetEnvIf으로 달성 될 수있다. 개발자 사이트에서 Facebook OpenGraph 태그의 작동 방식을 테스트해야했습니다.

# allow facebook external hit 
SetEnvIf user-agent "facebookexternalhit/1.1" allowthis 
# disallow some other stuff 
SetEnvIfNoCase Host "topasswordprotect\.domain\.com$" !allowthis 

AuthType Basic 
AuthName "dev access" 
AuthUserFile .htpasswd 
Require valid-user 

Allow from allowthis=1 
Satisfy Any 

IfDefine은 환경 변수와 같지 않은 정의에 따라 작동합니다.

인용 한

apache docs에서 :

매개 변수-name 인수는 서버가 시작된 시점에서, -Dparameter-를 통해 httpd 명령 행에 주어진으로 정의한다.