Forum Moderators: phranque
The problem: If you run WordPress from root and want to password protect some sub-directories, the default installation of WordPress makes this impossible by means of the rule WordPress inserts into .htaccess.
A default installation of WordPress adds the following lines to .htaccess:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
HOWEVER if you decide to password protect the directory /library the conditions do not apply. There is a good effort in explaining why and a fix over at the WordPress.org forum: [wordpress.org...]
The fix: Precede the rule in .htaccess with the following lines:
ErrorDocument 401 /path/to/onerror.html
ErrorDocument 403 /path/to/onerror.html
Where onerror.html is a blank html file.
My question (finally):
Is this a viable solution? Wouldn't it be better to have another rule for e.g. /Library that takes precedence over . /index.php [L], or is it fruitless since you cannot intercept the 401/403 response?
Any suggestions or comments are very much welcomed, thank you!
Best Regards
//ZM
Password-protection is always difficult to debug, and I don't have a site set up like this, nor time to test it. But I'd recommend doing it like this:
In /.htacess:
RewriteEngine on
# BEGIN WordPress (modified)
# If requested URL-path does not start with "/library/"
RewriteCond %{REQUEST_URI} !^/library/
# and requested URL_path does not resolve to existing file
RewriteCond %{REQUEST_FILENAME} !-f
# and requested URL-path does not resolve to existing directory
RewriteCond %{REQUEST_FILENAME} !-d
# rewrite the request to WordPress
RewriteRule . /index.php [L]
# END WordPress
Then add your password-protection code to example.com/library/.htaccess *and* declare a 401 ErrorDocument in that subdirectory as well:
ErrorDocument 401 /library/error-401.html
The new password-protection and errordocument code placed in /library/.htaccess will only apply to requests for that subdirectory, so interaction with WP shouldn't be a problem.
I'd also recommend adding error documents in /library and corresponding error pages for at least the following error conditions, in addition to 401-Authorization required:
400-Bad request (The server could not understand the request)
403-Forbidden (Access to the requested page is not allowed)
404-Not Found (Page is missing, reason is unknown, page may return later)
410-Gone (Page was intentionally removed, and will not return)
500-Server Error (Server is likely mis-configured, please try again later)
Note that many sites' functional problems and poor search engine ranking problems are caused by incomplete and/or incorrect server configuration -- It pays to be thorough in both configuration and testing!
One more note: While testing, I recommend that you completely flush your browser cache before testing any new changes to your code -- Stale cached server responses can cause a lot of problems and confusion in testing!
Jim
Your suggestions do indeed solve the problem.
Some findings:
Thanks in advance!
Best Regards
//ZM
Apparently here is what happens:
When checking if a destination exists,
* And if an .htaccess doc exists there
* And if it requires authentication
* And IF NO 401 DOC EXISTS
then the authentication cannot proceed,
and we are sent to WordPress's default 403 behaviour.
Simply by declaring and creating a valid 401 document
*not in* the protected directory, *then* the 'protected
directory' behavior works with the Wordpress code.
To summarize what I've done - and believe to be the minimal implementation:
I created 2 files:
/Forms/401.html
AUTHENTICATION REQUIRED
If you have been granted access,
you will have an email with user
and password details.
/protected-dir/.htaccess
ErrorDocument 401 /Forms/401.html
AuthType Basic
AuthName "Paid Member"
AuthUserFile "/home/acct/.htpasswds/public_html/protected-dir/passwd"
require valid-user
Cheers,
Tim
[edited by: jdMorgan at 4:33 pm (utc) on July 5, 2008]
[edit reason] No URLs, please -- See TOS. [/edit]