Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Apache .htaccess在多个环境中_Apache_.htaccess_Mod Rewrite_Expressionengine_Environments - Fatal编程技术网

Apache .htaccess在多个环境中

Apache .htaccess在多个环境中,apache,.htaccess,mod-rewrite,expressionengine,environments,Apache,.htaccess,Mod Rewrite,Expressionengine,Environments,我知道以前也有人问过类似的问题,但我还没有找到任何针对我的情况的具体答案 我有一个ExpressionEngine站点在多个环境(本地、开发、生产)上运行,每个环境都需要不同的访问规则: 所有环境 设置一个404文件 设置301重定向 发展 使用.htpasswd进行密码保护 强制HTTPS协议 防止使用搜索引擎索引 生产 强制HTTPS协议 将非www子域重定向到www 本地 没有唯一的规则 我已经看到了很多关于如何为每个模块设置特定环境的示例。类似于mod_rewrite模

我知道以前也有人问过类似的问题,但我还没有找到任何针对我的情况的具体答案

我有一个ExpressionEngine站点在多个环境(本地、开发、生产)上运行,每个环境都需要不同的访问规则:

所有环境

  • 设置一个404文件
  • 设置301重定向
发展

  • 使用.htpasswd进行密码保护
  • 强制HTTPS协议
  • 防止使用搜索引擎索引
生产

  • 强制HTTPS协议
  • 将非www子域重定向到www
本地

  • 没有唯一的规则
我已经看到了很多关于如何为每个模块设置特定环境的示例。类似于mod_rewrite模块和.htpasswd需求的
RewriteCond%{REQUEST_HOST}^dev.myurl.com

但我真正喜欢的是某种方式来设置全局环境变量,然后在每个环境的.htaccess文件中重用这些变量。以伪javascript为例,如下所示:

var local = 'mysite.local';
var development = 'dev.mysite.com';
var production = 'www.mysite.com';

// Global .htaccess rules

if(environment == local){
   // Local environment .htaccess rules
}

if(environment == development){
   // Development environment .htaccess rules
}

if(environment == production){
   //Production envirotnment .htaccess rules
}
# Global htaccess rules
RewriteEngine On
RewriteRule ^foo$ /bar [L]

# Only local
<IfDefine LocalInstance>
    RewriteRule ^local/foo /bar [L]
</IfDefine>

# Only dev
<IfDefine DevInstance>
    RewriteRule ^dev/foo /bar [L]
</IfDefine>

# Only production
<IfDefine ProductionInstance>
    RewriteRule ^dev/foo /bar [L]
</IfDefine>
通过这种方式,所有特定于环境的规则都被分组在一起,形成一个真正干净的文件,如果环境发生变化,则只需要更改一个变量

我在Apache的配置文件中看到了一些关于更改设置的引用,但如果我处理第三方主机,显然这不是一个可行的选项

那么,这是天方夜谭的一厢情愿,还是可以实现

那么,这是天方夜谭的一厢情愿,还是可以实现

对。您永远无法获得基于ENV变量或类似内容的规则的可预测“范围”。apache中不存在任意的
if(something){do everything in here}
。很多指令在特定的范围内都不起作用,在以后,当您需要更改某些内容的工作方式时,您更有可能破坏现有的内容,而不是简单地修改它

最好的方法是:

如果可以访问httpd主服务器配置文件,则应避免完全使用.htaccess文件。使用.htaccess文件会降低Apache http服务器的速度。可以包含在.htaccess文件中的任何指令最好在
目录
块中设置,因为它具有相同的效果和更好的性能

为本地、开发和生产创建单独的vhost。根据需要打开或关闭它们,无论它们共享什么全局配置,将其存储在其他位置(如在名为
global.includes
的文件中),然后在所有3个vhost中使用
Include
指令。如果需要将规则应用于特定目录,请使用
块而不是htaccess文件

如果您希望将所有内容都放在htaccess文件中,您可以尝试将所有内容放在htaccess文件中,这可能是您在问题中最接近伪代码的内容。基本上类似于:

var local = 'mysite.local';
var development = 'dev.mysite.com';
var production = 'www.mysite.com';

// Global .htaccess rules

if(environment == local){
   // Local environment .htaccess rules
}

if(environment == development){
   // Development environment .htaccess rules
}

if(environment == production){
   //Production envirotnment .htaccess rules
}
# Global htaccess rules
RewriteEngine On
RewriteRule ^foo$ /bar [L]

# Only local
<IfDefine LocalInstance>
    RewriteRule ^local/foo /bar [L]
</IfDefine>

# Only dev
<IfDefine DevInstance>
    RewriteRule ^dev/foo /bar [L]
</IfDefine>

# Only production
<IfDefine ProductionInstance>
    RewriteRule ^dev/foo /bar [L]
</IfDefine>
#全局访问规则
重新启动发动机
重写规则^foo$/bar[L]
#只有本地的
重写规则^local/foo/bar[L]
#只有dev
重写规则^dev/foo/bar[L]
#只生产
重写规则^dev/foo/bar[L]

然后,在启动apache时,需要将
-DLocalInstance
-DDevInstance
-dproductionstance
作为命令行参数传递,或使用vhost配置中的某个地方的
Define
指令(只有一个参数)。这并不能保证像它看起来应该的那样顺利工作,我以前在
中遇到过无法解释的问题,尤其是当你试图变得太花哨的时候。

乔恩的答案很好。不幸的是,并不是所有的web主机都允许您控制-D参数来启动Apache

以下是一种在开发和生产环境中使用单个htaccess文件的方法,但只保护开发人员站点密码:

# ----------------------------------------------------------------------
# Password protect staging server
# Use one .htaccess file across multiple environments
# (e.g. local, dev, staging, production)
# but only password protect a specific environment.
# ----------------------------------------------------------------------

SetEnvIf Host staging.domain.com passreq
AuthType Basic
AuthName "Password Required"
AuthUserFile /full/path/to/.htpasswd
Require valid-user
Order allow,deny
Allow from all
Deny from env=passreq
Satisfy any

在Apache HTTP Server v2.4中,一个核心增强功能是:
,可以使用节根据每个请求标准设置配置。
只要您想要使用的支持目录上下文。