InfoHeap
Tech
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

Apache tutorials

  • AWStats on Ubuntu
  • ApacheBench - load testing
  • Auth to a location or directory
  • Block directory access using htaccess 404
  • Different expire Headers for multiple images
  • Disable directory listing
  • List loaded modules
  • Log Content-Type in access log
  • Log latency and host in apache log
  • Monitoring using mod_status
  • Python to analyze bots in logs
  • Remove php extension from url
  • egrep and access log
  • log custom data in apache access log
  • mod_rewrite
  • top IP list from access log
 
  • Home
  • > Tutorials
  • > Web Development
  • > Apache

Apache – add basic auth to a location or directory

By admin on Nov 14, 2015

In case you want to protect a directory from public access, you can add Apache basic auth to it. This may be needed if you have a wordpress or some other software and want to keep some internal scripts behind auth. Here are quick steps to do this using Apache or .htaccess configuration.

Apache config for basic auth

Use the following code if you want to add basic auth to a location /internal which is served by directory internal inside Apache document root.

<Location /internal>
  AuthType Basic
  AuthName "Restricted Resource"
  AuthBasicProvider file
  AuthUserFile /etc/auth-file
  Require valid-user
</Location>

htaccess config for basic auth

Use the following code in .htaccess in directory internal.

AuthType Basic
AuthName "Restricted Resource"
AuthBasicProvider file
AuthUserFile /etc/auth-file
Require valid-user

Using htpasswd to create auth user

To add/edit auth users to /etc/auth-file you can use the command line utility htpasswd. Here is the command you can use in case auth file /etc/auth-file is being created first time:

sudo htpasswd -c /etc/auth-file awstats

Incase file already exists:

sudo htpasswd /etc/auth-file awstats

View an auth protected url

When the above location is visited in browser, here is how the basic auth is asked by browser.
apache-authentication-required-example-in-chromeAny user and password mentioned in /etc/auth-file can be used for authentication.

Video version of the tutorial

Here is the video tutorial on same topic (courtesy Webucator – Online and onsite instructor led training classes). You may also want to checkout their Apache courses.

Suggested posts:

  1. How to use ssh port forwarding to surf a site from different location
  2. Why and how to log Content-Type in apache access log
  3. PHP sort associative array using custom compare function
  4. Block directory access using htaccess 404
  5. How to list git branches
  6. Bash – how to use functions – quick tutorial
  7. PHP execute command and capture shell exit status
  8. node – how to fix cannot find module error
Share this article: share on facebook share on linkedin tweet this submit to reddit
Posted in Tutorials | Tagged Apache, Linux, Tutorials, Ubuntu Linux, Web Development
  • Browse content
  • Article Topics
  • Article archives
  • Contact Us
Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | Company results | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries | InfoHeap Money

Copyright © 2025 InfoHeap.

Powered by WordPress