developer tip

htaccess에서 AuthUserFile과 함께 RELATIVE 경로를 사용하는 방법은 무엇입니까?

optionbox 2020. 9. 17. 07:41
반응형

htaccess에서 AuthUserFile과 함께 RELATIVE 경로를 사용하는 방법은 무엇입니까?


기본 인증을 사용하는 .htaccess가 있습니다. .htpasswd 파일의 경로가 htaccess 파일에 상대적이 아니라 서버 구성에 상대적인 것 같습니다.

따라서 동일한 디렉토리에 .htaccess 및 .htpasswd 파일이 있어도 작동하지 않습니다.

AuthType Basic
AuthName "Private Login"
AuthUserFile .htpasswd
Require valid-user

그러나 절대 경로를 사용하도록 AuthUserFile을 변경하면 작동합니다.

AuthType Basic
AuthName "Private Login"
AuthUserFile "/home/user/public_html/mydir/.htpasswd"
Require valid-user

그러나 나는 다른 지역의 여러 사이트에서 이것을 사용하기 때문에 더 많은 모바일을 선호합니다. 웹을 검색했지만 해결책이 없습니다. 상대 경로 또는 변수를 사용할 수 %{DOCUMENT_ROOT}있습니까?


AuthUserFile에 대해 상대 경로를 사용할 수 없습니다 .

파일 경로는 사용자 파일의 경로입니다. 절대적이지 않은 경우 (예 : 슬래시로 시작하지 않는 경우) ServerRoot.

그 제한을 받아들이고 해결해야합니다.


우리가 사용하고있는 IfDefine아파치와 함께 명령 줄 매개 변수 :

.htaccess (개발 및 라이브 시스템 모두에 적합) :

<IfDefine !development>
  AuthType Basic
  AuthName "Say the secret word"
  AuthUserFile /var/www/hostname/.htpasswd
  Require valid-user
</IfDefine>

개발 서버 구성 (Debian)

다음에 다음을 추가하십시오 /etc/apache2/envvars.

export APACHE_ARGUMENTS=-Ddevelopment

나중에 아파치를 다시 시작하면 개발 서버에 있지 않을 때만 암호 프롬프트가 표시됩니다.

물론 개발 서버에 다른 IfDefine을 추가 할 수 있습니다 !. 블록을 복사하고 .


1) .htpasswd서버 루트 아래에 파일 이있는 것은 안전하지 않은 것으로 간주됩니다 .

2) 문서 는 상대 경로에 대해 이렇게 말하고 있으므로 운이 좋지 않은 것 같습니다.

파일 경로는 사용자 파일의 경로입니다. 절대적이지 않은 경우 (즉, 슬래시로 시작하지 않는 경우) ServerRoot에 상대적인 것으로 처리됩니다 .

3) 환경 변수 사용을 권장하는 답변은 완벽하게 작동하지만 .htaccess파일에 자리 표시자를 넣거나 코드베이스에 다른 버전을 사용하고 배포 프로세스에서 모두 설정 하도록하는 것이 좋습니다 (예 : 자리 표시 자 교체 또는 이름 바꾸기 / 적절한 파일 이동).

Java 프로젝트에서는 Maven을 사용하여 이러한 유형의 작업을 수행합니다. 예를 들어 PHP 프로젝트에서 배포 된 파일을 환경에 맞게 조정하는 build.sh 및 / 또는 install.sh 셸 스크립트가 있습니다. 이는 코드베이스를 대상 환경의 세부 사항 (즉, 환경 변수 및 구성 매개 변수)에서 분리합니다. 일반적으로 응용 프로그램은 환경에 적응해야합니다. 반대로 수행하면 환경이 다른 응용 프로그램이나 완전히 관련이없는 시스템 특정 요구 사항을 충족해야하는 경우 문제가 발생할 수 있습니다.


사람들이 이에 대한 해결책을 찾고있는 경우를 위해 :

<If "req('Host') = 'www.example.com'">
    Authtype Basic
    AuthName "user and password"
    AuthUserFile /var/www/www.example.com/.htpasswd
    Require valid-user
</If>

인증 설정을 환경에 넣을 수 있습니다. 처럼:

SetEnvIf HTTP_HOST testsite.local APPLICATION_ENV=development
<IfDefine !APPLICATION_ENV>
  Allow from all
  AuthType Basic
  AuthName "My Testseite - Login" 
  AuthUserFile /Users/tho/htdocs/wgh_staging/.htpasswd
  Require user username
</IfDefine>

인증은 작동하지만 내 환경을 실제로 실행할 수 없습니다.


.htpasswd에는 서버의 절대 루트로부터의 전체 절대 경로가 필요합니다.

Please get full absolute path of the file by echo echo $_SERVER['DOCUMENT_ROOT'];.

here is working basic auth .htaccess script.

AuthType Basic
AuthName "Access to the Hidden Files"
AuthUserFile 'C:/xampp/htdocs/ht/.htpasswd'
Require valid-user

Before login

enter image description here

Afetr Login

enter image description here


If you are trying to use XAMPP with Windows and want to use an .htaccess file on a live server and also develop on a XAMPP development machine the following works great!


1) After a fresh install of XAMPP make sure that Apache is installed as a service.

  • This is done by opening up the XAMPP Control Panel and clicking on the little red "X" to the left of the Apache module.
  • It will then ask you if you want to install Apache as a service.
  • Then it should turn to a green check mark.

2) When Apache is installed as a service add a new environment variable as a flag.

  • First stop the Apache service from the XAMPP Control Panel.
  • Next open a command prompt. (You know the little black window the simulates DOS)
  • Type "C:\Program Files (x86)\xampp\apache\bin\httpd.exe" -D "DEV" -k config.
  • This will append a new DEV flag to the environment variables that you can use later.

3) Start Apache

  • Open back up the XAMPP Control Panel and start the Apache service.

4) Create your .htaccess file with the following information...

<IfDefine DEV>
  AuthType Basic
  AuthName "Authorized access only!"
  AuthUserFile "/sandbox/web/scripts/.htpasswd"
  require valid-user
</IfDefine>

<IfDefine !DEV>
  AuthType Basic
  AuthName "Authorized access only!"
  AuthUserFile "/home/arvo/public_html/scripts/.htpasswd"
  require valid-user
</IfDefine>

To explain the above script here are a few notes...

  • My AuthUserFile is based on my setup and personal preferences.
  • I have a local test dev box that has my webpage located at c:\sandbox\web\. Inside that folder I have a folder called scripts that contains the password file .htpasswd.
  • The first entry IfDefine DEV is used for that instance. If DEV is set (which is what we did above, only on the dev machine of coarse) then it will use that entry.
  • And in turn if using the live server IfDefine !DEV will be used.

5) Create your password file (in this case named .htpasswd) with the following information...

user:$apr1$EPuSBcwO$/KtqDUttQMNUa5lGXSOzk.

A few things to note...


or if you develop on localhost (only for apache 2.4+):

<If "%{REMOTE_ADDR} != '127.0.0.1'">
</If>

I know this is an old question, but I just searched for the same thing and probably there are many others searching for a quick, mobile solution. Here is what I finally come up with:

# We set production environment by default
SetEnv PROD_ENV 1

<IfDefine DEV_ENV>
  # If 'DEV_ENV' has been defined, then unset the PROD_ENV
  UnsetEnv PROD_ENV

  AuthType Basic
  AuthName "Protected Area"
  AuthUserFile /var/www/foo.local/.htpasswd
  Require valid-user
</IfDefine>

<IfDefine PROD_ENV>
  AuthType Basic
  AuthName "Protected Area"
  AuthUserFile /home/foo/public_html/.htpasswd
  Require valid-user
</IfDefine>

Let's take an example.

Your application is located in /var/www/myApp on some Linux server

.htaccess : /var/www/myApp/.htaccess

htpasswdApp : /var/www/myApp/htpasswdApp. (You're free to use any name for .htpasswd file)

To use relative path in .htaccess:

AuthType Digest
AuthName myApp
AuthUserFile "htpasswdApp"
Require valid-user

But it will search for file in server_root directory. Not in document_root.

In out case, when application is located at /var/www/myApp :

document_root is /var/www/myApp

server_root is /etc/apache2 //(just in our example, because of we using the linux server)

You can redefine it in your apache configuration file ( /etc/apache2/apache2.conf), but I guess it's a bad idea.

So to use relative file path in your /var/www/myApp/.htaccess you should define the password's file in your server_root.

I prefer to do it by follow command:

sudo ln -s /var/www/myApp/htpasswdApp /etc/apache2/htpasswdApp

You're free to copy my command, use a hard link instead of symbol,or copy a file to your server_root.

참고URL : https://stackoverflow.com/questions/6111627/how-to-use-a-relative-path-with-authuserfile-in-htaccess

반응형