Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I do an if/then/else in htaccess?

I have a site that serves up certain content based on the subdomain. So it is the same set of files, and maybe the header and some info in the site pages changes based on the subdomain.

I need to have different htpassword authentication based on the subdomain as well, but can't find info on how to do an if/then type of thing in htaccess .

Basically what I need is this:

if subdomain = 'abc' use this htpassword file

if subdomain = 'def' use this htpassword file

Can this be done?

like image 893
user151257 Avatar asked Aug 05 '09 18:08

user151257


People also ask

What is Rewrite condition in htaccess?

htaccess rewrite rules can be used to direct requests for one subdirectory to a different location, such as an alternative subdirectory or even the domain root. In this example, requests to http://mydomain.com/folder1/ will be automatically redirected to http://mydomain.com/folder2/.

How to write Rewrite rule in htaccess?

htaccess rewrite rule includes setting a combination of rewrite condition ( RewriteCond ) tests along with a corresponding rule ( RewriteRule ) if the prior conditions pass. In most cases, these rules should be placed at any point after the RewriteEngine on line in the . htaccess file located in the website's docroot.

Can you have multiple htaccess?

You can have more than one . htaccess file on your hosting account, but each directory or folder can only have one. For example, you can have separate . htaccess files in your root folder and another in a sub-folder.

What is RewriteCond and RewriteRule?

There are two main directive of this module: RewriteCond & RewriteRule . RewriteRule is used to rewrite the url as the name signifies if all the conditions defined in RewriteCond are matching. One or more RewriteCond can precede a RewriteRule directive.


1 Answers

Try something like this:

SetEnvIf Host ^abc\. HOST_ABC
SetEnvIf Host ^dev\. HOST_DEF

<IfDefine HOST_ABC>
    AuthUserFile …
</IfDefine>
<IfDefine HOST_DEF>
    AuthUserFile …
</IfDefine>
like image 166
Gumbo Avatar answered Oct 14 '22 08:10

Gumbo