Uygulamanız için bir login kodu yazacağınıza, sadece .htaccess ve .htpasswd kullanarak bu işlemi gerçekleştirebilirsiniz. Daha fazla bilgi için, burayı ve burayı okuyabilirsiniz.


Senaryo


İstediğimiz şey:



Site yapısı


hello
site-1
index.php
site-2
index.php
index.php

Gerekli olan .htaccess ve .htpasswd dosyaları


Bu link ile .htaccess dosyası ve bu link ile .htpasswd dosyası yaratabilirsiniz. Varsayalım ki projenin ana yolu /var/www/html/local/hello/ ve elimizdeki kullanıcılar user-1 ve user-2. Yarattığımız dosyalar aşağıdaki gibi olmalılar.


.htaccess


Projenin ana yoluna gider.


AuthType Basic
AuthName "Protected Site"
AuthUserFile /var/www/html/local/hello/.htpasswd
Require valid-user

.htpasswd


Projenin ana yoluna gider.


user-1:$apr1$CraA.0n7$JRqS7GyggMKNYcTP65rAW/
user-2:$apr1$2QIlucFc$auT3J0uJmBjhY0axRrVnJ.

user-1'in .htaccess dosyası


Projenin site-1 klasörüne gider.


AuthUserFile /var/www/html/local/test/.htpasswd
Require user user-1

user-2'nin .htaccess dosyası


Projenin site-2 klasörüne gider.


AuthUserFile /var/www/html/local/test/.htpasswd
Require user user-2

VirtualHost ayarları


inanzzz@inanzzz:/ $ sudo nano /etc/apache2/sites-available/hello.local.conf

# Config content
<VirtualHost *:80>
ServerName hello.local
ServerAlias www.hello.local
ServerAdmin admin@hello.local

DocumentRoot /var/www/html/local/hello

<Directory /var/www/html/local/hello>
AllowOverride AuthConfig
Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec
Order allow,deny
Allow from all
</Directory>

ErrorLog ${APACHE_LOG_DIR}/hello.local.error.log
CustomLog ${APACHE_LOG_DIR}/hello.local.access.log combined
</VirtualHost>

inanzzz@inanzzz:/ $ sudo a2ensite hello.local.conf
inanzzz@inanzzz:/ $ sudo nano /etc/hosts

# Hosts content
127.0.0.1 hello.local

inanzzz@inanzzz:/ $ sudo services apache2 restart

Sitenin final yapısı


hello
site-1
index.php
.htaccess # user-1 specific one
site-2
index.php
.htaccess # user-2 specific one
index.php
.htaccess
.htpasswd

Ana index.php


if (isset($_SERVER['PHP_AUTH_USER']) && in_array($_SERVER['PHP_AUTH_USER'], ['user-1', 'user-2'])) {
header('location: /'.substr($_SERVER['PHP_AUTH_USER'], -1));
exit;
}

Test


Eğer http://hello.local giderseniz, login bilgilerinizi girmeniz gerekecek. Girdiğiniz bilgilere göre, sadece ulaşım hakkına sahip olduğunuz klasör ve kaynaklara yönlendirilirsiniz. Diğer yerlere ulaşamazsınız.