Talks on Web Technology and Better Product Development
Hello!
Recently a Canadian client of mine told me to look at an interesting wordpress problem. I regularly develop wordpress theme for him. He is a great artisitic designer of wordpress theme and I code theme to HTML & CSS. Whatever, my client told me that he wants to password protect a sub-directory “download”. He has done it from cPanel. He also created a user and permitted that user in that sub-directory. But it was not working.
Befoer I start, I want to clear that in this case wordpress was installed in root directory. One more thing is in this post i cannot write “.” and “htaccess” together. May be this is a security issue. If I write “.” and “htaccess” together, the post is not saved and I get a 404 error. So, I have written (dot)htaccess instead. Again, (dot)htpasswds instead of “.” and “htpasswds” together.
So I logged in to the cPanel to check what is happening actually. I found that whenever you password protect a sub-directory from cPanel, a new (dot)htaccess file will be created in that sub-directory. And the content of the (dot)htaccess file is less or more like as follows:
AuthType Basic AuthName “Authorized Area!”
AuthUserFile “/home/ftpusername/(dot)htpasswds/public_html/download/passwd”
require valid-user
And your password that you set from the cPanel will be encrypted and saved in a file (dot)htpasswds in the AuthUserFile location.
If this was not a wordpress site or it was a wordpress site (but custom Permalink not used; rather the Default used), you should not face problem in most cases. But when you use customized Permalink in wordpress to create beautiful links and try to protect a password, the password protection from cPanel does not work!
But do you know why? Here is the reason: in the root directory you installed wordpress and here there is a (dot)htaccess file. The settings in this file and the (dot)htaccess in your sub-directory (for example root_directory/download/(dot)htaccess) do not smoothly match with each other with since you are using Permalink plug-in to customize your URLs.
So, when you try to go to: http://www.yoursitename.com/download wordpress think that it is a post or page!
Wordpress think this because you have enables permalink and this is quite correct and logical. But you know your download sub-directory is not a post but a folder/directory in the root folder. So, you need to do something so that wordpress does not confuse the following link as a post or page.
http://www.yoursitename.com/download
Here is the solution:
1. Open the (dot)htaccess file in http://www.yoursitename.com/(dot)htaccess
2. Find out the line # BEGIN WordPress
3. Just before this, add the following three lines:
# To bypass download directory
ErrorDocument 401 /%{REQUEST_URI}/myerror.html
ErrorDocument 403 /%{REQUEST_URI}/myerror.html
4. Save the (dot)htaccess file and upload in your root or installation directory.
Now go to http://www.yoursitename.com/download and check if it works. If everything is OK, you should get a prompt asking for a username and password that you set from the cPanel.
Here is my example:
Sub-directory download password protected: http://www.tanzilo.com/download
Permalink smoothly working: http://www.tanzilo.com/2008/10/18/password-protect-sub-directory-in-wordpress-by-htaccess/
Or you may install a shopping cart in location as in:
http://www.yoursitename.com/shop
This should work smoothly too as you see my example here:
http://tanzilo.com/shop
Although there is an application/software in the shop sub-directory, wordpress no more thinks it a post or page!
Thus, you can set (dot)htaccess, password protected sub-directory (i.e. /download/) and any other sub-directory (i.e. /shop/) in such a way that they will be living in happiness in the same home!
The last interesting thing is this solution is sometimes helpful in Drupal and in some other applications where (dot)htaccess and URL rewriting code do not fit together.
Thanks for reading.
I am a website designer and programmer. I started this to share information, techniques and knowledgles with other people that can benefit them. I am planning to cover PHP, MySQL, JavaSript, AJAX, DHTML, CSS and XML. All these are related to Web Design and development. I shall try my best to write on up-to-date information.
Directory
October 24th, 2008 at 6:41 pm
I have been searching on the net for directory and end up visiting your site. I really like the posts here, especially this one regarding ord protect sub-directory in wordpress by htaccess - Learning Is Fun. I already bookmarked your site and sure visit again.
Tatiana
October 24th, 2008 at 7:07 pm
Very useful post. where can i find more articles no this subject ?
Michael
November 29th, 2008 at 7:33 am
Genius, thanks for helping me to stop pulling my hair out
tony
December 17th, 2008 at 12:34 pm
nice blog!! I really liked this blog.your post are good trip for me Thanks for sharing (:
M Prindle
December 18th, 2008 at 8:51 pm
With WordPress v2.6 and older locking a sub directory like wp-admin worked with no issues. It’s only 2.7 and later that it’s broke. I don’t totally understand what the code is doing, but it works.