How to redirect to specific page if not logged in with .htaccess

前端 未结 5 892
没有蜡笔的小新
没有蜡笔的小新 2021-01-06 00:30

I am running apache2 and php5 in my windows PC.

I have protected my directory using .htaccess and.htpasswd. If login information is not set

5条回答
  •  轮回少年
    2021-01-06 01:03

    I got this to work with an approach similar to AJ's. My .htaccess file is very similar to the following:

    AuthUserFile /opt/www/htaccess
    AuthType Basic
    
    DirectoryIndex public.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"
    
    
    
        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"
    
    
    RewriteEngine On
    RewriteBase /
    
    RewriteCond %{HTTP:Authorization} !=""
    RewriteRule ^$ secret.txt [L]
    

    With this, the site behaves as follows:

    1) Access the base URL -> see content from public.txt. 2) Access /secret.txt -> prompted to authenticate, and shown the contents of secret.txt. 3) Access the base URL again -> see content from secret.txt.

    Using [L,R] instead of [L] will use a 302 response to handle the redirection. This is a good option if you want the redirection to be visible in the browser's location field.

提交回复
热议问题