<p>It's not doing it due to order of operations. Your browser hits the site, checks htaccess (in the webroot /html/), and then asks craft for whatever page. Craft goes hunting around, and returns your page from templates. And it's Craft (not you the user) accessing the templates, so it's always 'approved.'</p>

<p>Here's a potential solution. Take a look at this page - <a href="https://noahread.net/blog/create-password-protected-pages-in-craft" rel="nofollow noreferrer">https://noahread.net/blog/create-password-protected-pages-in-craft</a></p>

<p>It's how to create a single password-protected page. Uses a for a password. And that password is hidden in a craft variable, so no one can see it. If it returns successfully, you get content.</p>

<p>All that said, craft pro might be cheaper than the time spent building said form.  </p>

CC BY-SA 4.0
2020-01-28T17:42:01.637Z
2020-01-28T17:42:01.637Z
SamC
<p>I would like to set a .htpasswd on a folder within my templates directory. Here is what I did:</p>

<p>..create the .htpasswd file and place in directory above my public folder
..create a .htaccess file that points to the .htpassword file, and place it in the folder I want to protect, which is in my templates directory.</p>

<p>When I enter a url to access the page, it displays the page fine, but does not trigger the .htpasswd. If I move the folder to my root directory, everything works fine.</p>

<p>Why does this not work as a Craft template?</p>

CC BY-SA 4.0
2020-01-25T23:31:54.280Z
Gonna ask a couple stupid questions, because I might have an answer, but I'm clear what you're trying to accomplish - Why are you password protecting your templates directory? No browser can get to your config or templates, it can only get into your webroot. 

Are you just trying to password your site? Or protect certain pages?
SamC
CC BY-SA 4.0
2020-01-27T16:21:57.430Z
Sorry, should have provided a bit more detail. The client is a start-up non-profit who does not want to spring for a Craft Pro license at this point. One of the site features is a photo gallery, and they would like to upload images. That requires using Twig, which means using  a template. Need to password protect the "upload image" page for obvious reasons (not the entire template directory).
Roi Agneta
CC BY-SA 4.0
2020-01-28T17:35:48.473Z
Hm. Do the images auto-load into the slideshow?

To be honest, if it were my site, I'd make people send images in via something like wetransfer or dropbox (with lots of storage), and then have admin load them to the site - that way they can approve or reject images for a variety of reasons.
SamC
CC BY-SA 4.0
2020-02-13T16:58:02.967Z
Saw this new plugin in the store today - might be a simpler solution - https://plugins.craftcms.com/basicauth
SamC
<templating>
.htpasswd in template file
# .htpasswd in template file

(Asked by: username_0 on 2020-01-24)

I would like to set a .htpasswd on a folder within my templates directory. Here is what I did:

..create the .htpasswd file and place in directory above my public folder
..create a .htaccess file that points to the .htpassword file, and place it in the folder I want to protect, which is in my templates directory.

When I enter a url to access the page, it displays the page fine, but does not trigger the .htpasswd. If I move the folder to my root directory, everything works fine.

Why does this not work as a Craft template?

**Tags:** templating

> **Comments:**
> **username_1**: Gonna ask a couple stupid questions, because I might have an answer, but I'm clear what you're trying to accomplish - Why are you password protecting your templates directory? No browser can get to your config or templates, it can only get into your webroot.   Are you just trying to password your site? Or protect certain pages?  
> **username_0**: Sorry, should have provided a bit more detail. The client is a start-up non-profit who does not want to spring for a Craft Pro license at this point. One of the site features is a photo gallery, and they would like to upload images. That requires using Twig, which means using  a template. Need to password protect the "upload image" page for obvious reasons (not the entire template directory).  
> **username_1**: Hm. Do the images auto-load into the slideshow?  To be honest, if it were my site, I'd make people send images in via something like wetransfer or dropbox (with lots of storage), and then have admin load them to the site - that way they can approve or reject images for a variety of reasons.  
> **username_1**: Saw this new plugin in the store today - might be a simpler solution - https://plugins.craftcms.com/basicauth  

---

## 1 answers

### Answer by username_1 on 2020-01-28. Score: 1

It's not doing it due to order of operations. Your browser hits the site, checks htaccess (in the webroot /html/), and then asks craft for whatever page. Craft goes hunting around, and returns your page from templates. And it's Craft (not you the user) accessing the templates, so it's always 'approved.'

Here's a potential solution. Take a look at this page - <https://noahread.net/blog/create-password-protected-pages-in-craft>

It's how to create a single password-protected page. Uses a for a password. And that password is hidden in a craft variable, so no one can see it. If it returns successfully, you get content.

All that said, craft pro might be cheaper than the time spent building said form.


---

