Home » Wordpress » how to make the .htaccess rule work for specified domain ,subdomain or addons

how to make the .htaccess rule work for specified domain ,subdomain or addons

Posted by: admin November 30, 2017 Leave a comment

Questions:

This is my cpanel directory structure:

public_html
    |--->wordpress installed 

sudomain-directory
    |--->wordpress installed
addon-domain
    |--->wordpress installed

What I have done:
Based on user choice the passwd and .htaccess file will be created to respective domains inside wp-admin directory

.htaccess file

authname "member only"
authtype basic
authuserfile /home/taragurung/public_html/wp-admin/.passfile
require valid-user

Now when user try to access www.somedomain.com/wp-admin the authentication will pop up and it works as expected.

Problem:
The problem is it works only for wp-admin but not for the www.somedomain.com/wp-login

To make it work I added the <FilesMatch "wp-login.php"> rule and
created the .htaccess in the root directory

but doing so will make the rule apply to all the domains and subdomains

Why I didn’t put it in root directory:
I don’t want the rules to be implemented on all the subdomains and addon domains. Want it to be implemented on only those domains or sub-domain user choose to.

Answers:

I hope I’ve understood your question correctly – but you only want the authentication to work if the user directs from a specific domain.

To do this you can use an if directive.

So, if user x comes from the domain somedomain.com/wp-login then it will show the authentication, if they do not, then it will not show it.

<if "%{HTTP_HOST} == 'somedomain.com/wp-login'">
    authname "member only"
    authtype basic
    authuserfile /home/taragurung/public_html/wp-admin/.passfile
    require valid-user
</if>

I hope this helps and as always, clear your cache before testing this.