How Can I Have A Conditional .htaccess Block?

前端 未结 4 1978
说谎
说谎 2020-12-05 05:47

This is an Apache question you\'ve probably come across before. I want to have one source package that I can deploy to my workstation, my staging server, and my production s

相关标签:
4条回答
  • 2020-12-05 05:53

    Instead of using SetEnv, use the environment variable setting capabilities of RewriteRule itself:

    RewriteCond %{HTTP_HOST} =foo.com
    RewriteRule ^ - [E=VARNAME:foo]
    
    RewriteCond %{HTTP_HOST} =bar.com
    RewriteRule ^ - [E=VARNAME:bar]
    

    Although I prefer doing this sort of thing by passing flags to the httpd process at startup and looking for them using IfDefine blocks.

    <IfDefine FOO>
    SetEnv VARNAME foo
    </IfDefine>
    
    <IfDefine BAR>
    SetEnv VARNAME bar
    </IfDefine>
    
    0 讨论(0)
  • 2020-12-05 06:03

    Here is a simple example that should be enough for you to change it to meet your requirements:

    <IfModule mod_rewrite.c>
        RewriteEngine On
    
        RewriteBase /
    
        RewriteCond %{HTTP_HOST} !^localhost
        RewriteCond %{HTTP_HOST} !^www\.
        RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [R=301,L]
    </IfModule>
    
    0 讨论(0)
  • 2020-12-05 06:07

    On Ubuntu Linux, the IfDefine's variable is set in

    /etc/apache2/envvars
    

    and is called APACHE_ARGUMENTS. So, at the bottom of that file I had to add:

    export APACHE_ARGUMENTS="-D dev"
    

    ...and then bounce the server with:

    /etc/init.d/apache2 stop
    /etc/init.d/apache2 start
    

    On other systems:

    However, there's a Debian article on this topic that discusses this here. In that example, the file to edit is /etc/default/apache2 and the variable is called APACHE_DEFINES.

    Likewise, on some systems it is a variable named OPTIONS that is set in /etc/sysconfig/httpd.

    So, what you really need to do is look for the start section in your apache2ctl file. So, begin by doing a whereis apache2ctl to find where that script is, cat it out and find the start section with the apache2 directive in it, and see if the variable it passes is OPTIONS, APACHE_ARGUMENTS, APACHE_DEFINES, or something else. Then, see which file you need to edit by experimentation with either /etc/sysconfig/httpd, /etc/default/apache2, or /etc/apache2/envvars.

    0 讨论(0)
  • 2020-12-05 06:09

    I tried the IfDefine method and it didn't work, even though the defined variable i'd passed into the Apache startup was definitely showing up in phpinfo() on the APACHE_ARGUMENTS line.

    I tried another method and it worked perfectly. In your .htaccess file you need something like:

    # Is the domain local (i wanted to check two names)? <If "%{SERVER_NAME} != 'localhost' && %{SERVER_NAME} != 'myproject.localhost'"> # I wanted to password protect the production server only AuthType Basic AuthName "Restricted area" AuthUserFile /app/.htpasswd require valid-user </If>

    I'm using Apache 2.4. Might not work for earlier versions.

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