Domain name specific code blocks in htaccess

前端 未结 3 1274
醉话见心
醉话见心 2021-02-04 03:29

We have local servers, central dev, staging and production servers. However the dev and staging are password protected for obvious reasons. So after deploying any changes to the

3条回答
  •  梦谈多话
    2021-02-04 03:53

    While doing it through the VirtualHost and not a .htaccess is much better, you can use the following solution if the setenvif module is active:

    SetEnvIf Host ^dev\.site\.com$ is_on_dev_site
    SetEnvIf Host ^staging\.site\.com$ is_on_dev_site
    Order deny,allow
    Deny from env=is_on_dev_site
    # require password if access would otherwise be denied
    Satisfy any
    # Put your password auth stuff here
    

    You could also do it with a whitelist which is probably better as it ensures your dev sites are still protected even if someone decides to allow access to them via www.dev.site.com etc.:

    SetEnvIf Host ^site\.com$ is_on_public_site
    SetEnvIf Host ^www\.site\.com$ is_on_public_site
    Order deny,allow
    Deny from all
    Allow from env=is_on_public_site
    Satisfy any
    # Put your password auth stuff here
    

    If you do not have mod_setenvif on your server, mod_rewrite can also do the work for you (replace the SetEnvIf blocks in the whitelist example with the following):

    RewriteEngine On
    RewriteCond %{HTTP_HOST} =site.com
    RewriteRule ^ - [E=is_on_public_site:yes]
    RewriteCond %{HTTP_HOST} =www.site.com
    RewriteRule ^ - [E=is_on_public_site:yes]
    

提交回复
热议问题