Menü schliessen
Created: December 6th 2019
Last updated: May 1st 2020
Categories: IT Development,  Wordpress
Author: Marcus Fleuti

Ask for htaccess Auth except for listed IP address

Tags:  Apache,  auth,  AuthType,  script,  wordpress
Donation Section: Background
Monero Badge: QR-Code
Monero Badge: Logo Icon Donate with Monero Badge: Logo Text
82uymVXLkvVbB4c4JpTd1tYm1yj1cKPKR2wqmw3XF8YXKTmY7JrTriP4pVwp2EJYBnCFdXhLq4zfFA6ic7VAWCFX5wfQbCC

In case that you want to use htaccess authorisation on the website, you should need two files:

  • .htaccess file
  • .htpasswd file,

In your .htpasswd file you would need to write your username and password, which looks something like this:

Somename:$apr1$8i60/hzd$J8U/qjNYHHASwkcDA29pq0
Someothername:$meh1$8i60/hzd$J8U/qjNYHHASwkcDA29ss1

Each line contains a username and a password separated by a colon ":". Passwords are encripted and in order to create them you can use some online tool for that or create it yourself with few lines of PHP code:

<?php
$myPassword = 'some awesome password';
$encripted_password = crypt($myPassword , base64_encode($myPassword ));
?>

Your .htaccess file will contain the following code:

AuthType Basic  
AuthName "restricted area"  
AuthUserFile /path/to/your/password_file/.htpasswd
Require valid-user

Now your authorisation works. But if you want to allow direct access to your website based on the IP address: code in the .htaccess file should look like this:

AuthType Basic
AuthName "restricted area"
AuthUserFile /path/to/your/password_file/.htpasswd
<RequireAny>
    Require ip 111.222.333.444
    Require valid-user
</RequireAny>

where 111.222.333.444 is the IP address which will be excluded from authorisation.