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

情到浓时终转凉″ 提交于 2019-11-29 10:36:28

Try something like this:

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

<IfDefine HOST_ABC>
    AuthUserFile …
</IfDefine>
<IfDefine HOST_DEF>
    AuthUserFile …
</IfDefine>

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.

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.

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)
...

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!