Domain name specific code blocks in htaccess

前端 未结 3 1272
醉话见心
醉话见心 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:49

    I found a nice solution, to distinguish "localhost" vs. "live":

    Since the conditionals of htaccess are somewhat limited why not settle for IfModule?: Compare the modules you have (i.e. using , and look for a significant, hopefully long-term difference, e.g. if you develop on windows and deploy on linux mod_win32.c might be good. ( Don't forget to add the .c which phpinfo() ommits.)

    Then you can go about it like this (tested):

    <IfModule mod_win32.c> 
        RewriteRule ^banana$ test.php?dudeThisIsLocal=1
    </IfModule>
    <IfModule !mod_win32.c> 
        RewriteRule ^banana$ test.php?dudeThisIsLive=1
    </IfModule>
    

    This example makes for a good sanity test, browing to yourdomain/banana resp. localhost/banana and if you (having rewrite enabled ) dump the $_GET array in test.php. If this works, fill the codeforks with your real config statements.

    0 讨论(0)
  • 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]
    
    0 讨论(0)
  • 2021-02-04 04:09

    You should do this in your sites conf file with:

    <VirtualHost domain.com:80>
    
    ...config statements here
    
    </VirtualHost>
    

    and

    <VirtualHost domain2.com:80>
    
    ....config statements here
    
    </VirtualHost>
    

    If you are with a host who does not allow you to edit your sites config file, which is a real possibility if you are with a shared host then you should consider VPS or dedicated hosting.

    0 讨论(0)
提交回复
热议问题