7

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?

Kev
  • 118,037
  • 53
  • 300
  • 385
user151257
  • 73
  • 1
  • 3

5 Answers5

4

Try something like this:

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

<IfDefine HOST_ABC>
    AuthUserFile …
</IfDefine>
<IfDefine HOST_DEF>
    AuthUserFile …
</IfDefine>
Gumbo
  • 643,351
  • 109
  • 780
  • 844
  • This looks great - I ended up having to just build a simple auth in PHP due to time constraints, but I would like to try this out in a test environment as it seems to make sense. Thanks! – user151257 Aug 07 '09 at 15:26
  • 5
    as charming as it looks, it didn't work for me. would u revise this Gumbo? – Devrim Dec 15 '09 at 19:02
0

which webserver are you using?

a technique that may work on several different servers (it certainly works in apache) is to have a different .htaccess file in the directory corresponding to each subdomain.

frankster
  • 1,529
  • 2
  • 16
  • 20
  • According to the question as posted, it's the same set of files, so there aren't separate directories. – Amber Aug 05 '09 at 18:31
  • Its a Win 2003 server using IIS with IISpassword and ISAPIrewrite3. Your solution won't work because there aren't physically different file sets in the filesystem for the subdomains - all the subdomains point to the same set of files and the PHP evaluates the subdomain and shows different data based on that. Thanks.. – user151257 Aug 05 '09 at 18:32
0

If it were me (and I haven't thought to do this before) I would:

  1. Use an internal redirect using mod_rewrite to different site roots (/suba or /subb) depending on some factors (emulating IF/ELSE)
  2. Use symlinks on the public directory, but not the .htaccess

so:

/sites/subdomain_a/.htaccess
/sites/subdomain_a/pub
/sites/subdomain_b/.htaccess
/sites/subdomain_b/pub

where

/sites/subdomain_a/pub == /sites/subdomain_b/pub

using

ln -s

or similar. Then use mod_rewrite to redirect (internally) to the right site root. I think mod_rewrite is run before .htaccess is loaded.

This means you are effectively serving the same site files with a different .htaccess depending on the subdomain.

Hope this helps, bit of a hack.

Would be interested to see if something better exists.

Aiden Bell
  • 28,212
  • 4
  • 75
  • 119
0

You can't use if/else because htaccess isn't a list of instructions. But, if you use a conditional block like <IfModule mod_rewrite.c> or <IfDefine something>, you can use different directives based on which subdomain is being requested. I'm not sure which one you would use though...

You could also put one .htaccess file one level above the files and use <Directory something> to specify different rules for each subdomain, but make the subdomain directories all be symlinks to the same directory with the shared files.

so this would be the directory above the web root(s):
.htaccess
sitefiles
subdomain1 (symlink to sitefiles)
subdomain2 (symlink to sitefiles)
...

rpjohnst
  • 1,612
  • 14
  • 21
0

You can use pseudo else with an exclamation mark. See the second directive in the example below MemCache vs !MemCache:

<IfDefine MemCache>
   LoadModule mem_cache_module   modules/mod_mem_cache.so
</IfDefine>
<IfDefine !MemCache>
   LoadModule cache_disk_module   modules/mod_cache_disk.so
</IfDefine>

Taken from the apache docs: https://httpd.apache.org/docs/2.4/mod/core.html#ifdefine

Adam Genshaft
  • 756
  • 10
  • 22