Code Snippet

Home » Code Snippets » HTAccess » Allow Single URL Through .htaccess Password Protection

Allow Single URL Through .htaccess Password Protection

This code is useful for multi environment setups (staging, production, etc.) it allows you to keep your htaccess files in sync while maintaining an htpasswd on your development environment or anything but the live environment.

#allows a single uri through the .htaccess password protection
SetEnvIf Request_URI "/testing_uri$" test_uri

#allows everything if its on a certain host
SetEnvIf HOST "^testing.yoursite.com" testing_url
SetEnvIf HOST "^yoursite.com" live_url
Order Deny,Allow

AuthName "Restricted Area"
AuthType Basic
AuthUserFile /path/to/your/.htpasswd
AuthGroupFile /
Require valid-user

#Allow valid-user
Deny from all
Allow from env=test_uri
Allow from env=testing_url
Allow from env=live_url
Satisfy any

Subscribe to The Thread

  1. Fugazer

    I found your .htaccess setup to be pretty good, but I found it more useful to allow from all then deny from staging.

    Below is the reverse of what your have in this article.

    Keep up the great work!


    #-- Staging Server Password -------------------------#
    SetEnvIf Host yourstagingserver.com passreq
    AuthType Basic
    AuthName "Staging Server"
    AuthUserFile /home/username/.htpasswd
    AuthGroupFile /home/username/.htgroup
    Require valid-user
    Order allow,deny
    Allow from all
    Deny from env=passreq
    Satisfy any

  2. Braulio

    Hi,
    I am trying to do something similar, thought I’m not sure if it’s possible. I have an images directory on my server and I would like the images to be accessed only by one of my php pages, preventing hotlinking and direct access. For instance:

    Directory:
    http://www.mysite.com/images/
    containing the files: 001.jpg / 002.jpg / etc

    Page:
    http://www.mysite.com/images.php?file=001
    to view file 001.jpg

    Any ideas of how should I configure my htaccess file?
    PS: I don’t know if it’s important since you are using REQUEST_URI, but I have multiple domains on my server (ex. http://www.mysite.com and http://www.mysite.net)

  3. George

    Yep, thats exactly what I am searching for since sunday. hotlinking from one server only to only one single url.
    @BRAULIO did you find a solution ??

  4. NobodyCares

    May try if these suggestions works for you George

    About midway through the page is a section about hotlinking images.

    http://www.gwizit.com/articles/getout.php

    Seemed to help my site from getting hotlinked any further.

  5. I am going through the book digging into wordpress and I copied and pasted the index.php file to the root and made the adjustments accordingly … I then checked the settings in the admin dash …

    I forgot to include the .htaccess file though … I looked for it … and didnt find it in the core files … so I made one with http://www.coffeecup.com s .htaccess software … but when I went to refresh the page … I kept getting a log in prompt just to view the home page … or make changes in the dash … how do I include the .htaccess without having to “log in” everysingle time?

Speak, my friend

At this moment, you have an awesome opportunity* to be the person your mother always wanted you to be: kind, helpful, and smart. Do that, and we'll give you a big ol' gold star for the day (literally).

Posting tips:
  • You can use basic HTML
  • When posting code, please turn all
    < characters into &lt;
  • If the code is multi-line, use
    <pre><code></code></pre>
Thank you,
~ The Management ~