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

前端 未结 5 893
没有蜡笔的小新
没有蜡笔的小新 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条回答
  •  旧时难觅i
    2021-01-06 01:11

    Answer explanation :

    You need to implement a custom authentication, natively you can not redirect on authentication fail.

    Solution :

    A custom ErrorDocument implementation using an HTML meta tag to make the redirection possible on authentication fail and let the user access the protected area on authentication success (The server will always cast out a 401 on first load before entering the user and password because the browser is not expecting such authentication in the first place and get refused the access)

        AuthUserFile /path/to/users
        AuthName "Access Denied"
        AuthGroupFile /dev/null
        AuthType Basic
        Require valid-user
    
        ErrorDocument 401 ""
    

    Alternative I :

    Since Apache 2.4. you can use mod_auth_form with htaccess to make an advanced authentication and use a more reliable solution

    http://httpd.apache.org/docs/trunk/mod/mod_auth_form.html

    Alternative II :

    Use a php to handle 401 ErrorDocument 401 /handle.php

    http://php.net/manual/en/features.http-auth.php

    Extended Security :

        ErrorDocument 401 ""
        ErrorDocument 400 /kickout.php
        ErrorDocument 403 /kickout.php
        ErrorDocument 500 /kickout.php
        Deny from all
        Allow from 192.200.x.x
        Allow from 192.200.x.x
        Allow from 127.0.0.1
        Allow from localhost
    

提交回复
热议问题