Home   /   DKMIS Blog   /   Codes

Password protect a specific URL

Posted on 31 January 2025 12:10 pm

To password-protect a specific URL, there are several methods depending on the type of server or framework you're using. Here are some common ways to password-protect a URL.

1. Using .htaccess on Apache Servers

If you're using an Apache server, you can set up basic HTTP authentication using a .htaccess file.

  1. Create a .htpasswd file with username and password:

    • You can create this file by running the following command:
    • htpasswd -c /path/to/.htpasswd username
    • Replace /path/to/.htpasswd with the path where you want the file saved, and username with the desired username.
    • This command will prompt you for a password and generate the file.
  2. Edit your .htaccess file in the directory containing the URL you want to protect:AuthType Basic
    AuthName "Restricted Access"
    AuthUserFile /path/to/.htpasswd
    Require valid-user
    Replace /path/to/.htpasswd with the actual path to the .htpasswd file.

  3. Test the URL: Visit the URL, and you should be prompted to enter the username and password.