的.htaccess在多环境 [英] .htaccess in Multiple Environments

查看:238
本文介绍了的.htaccess在多环境的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道类似的问题已经被问过,但我还没有发现我的情况真的任何具体的答案。

我有一个防爆pressionEngine网站上的多个环境(当地,开发,生产)上运行,而且每个环境都需要不同的.htaccess规则:

所有环境

发展

  • 密码保护与htpasswd的
  • 强制使用HTTPS协议
  • prevent搜索引擎索引与的X机器人-标记

制作

  • 强制使用HTTPS协议
  • 重定向非www子域到www

本地

  • 在没有独特的规则。

我已经看到了很多的如何设置每个模块具体环境的实例。像的RewriteCond%{REQUEST_HOST} ^ dev.myurl.com 的mod_rewrite的模块和的招数这样获取htpasswd的要求。

但我真的preFER一些方法来设置全局环境变量,然后在每个环境中的.htaccess文件重新使用这些变量。要使用伪JavaScript作为一个例子,是这样的:

  VAR本地='mysite.local';
VAR发展='dev.mysite.com';
VAR生产='www.mysite.com';

//全球的.htaccess规则

如果(环境==本地){
   //本地环境的.htaccess规则
}

如果(环境==发展){
   //开发环境的.htaccess规则
}

如果(环境==生产){
   //生产envirotnment的.htaccess规则
}
 

此方式,所有的环境特定的规则都是组合在一起,使得一个真正干净的文件,且只有一个变量需要,如果一个环境改变为被改变。

我已经看到了一些参考更改设置Apache的配置文件,但显然这不是如果我处理的第三方主机的一个可行的选择。

因此​​,这是乌托邦式的梦想的天空一厢情愿,或者能不能做到?

解决方案
  

因此​​,这是乌托邦式的梦想的天空一厢情愿,或者能不能做到?

国际海事组织,是的。你永远不会是能够得到predictable作用域的基础上ENV变量或类似的东西的规则。不存在任意如果(某事){尽在这里} 在Apache中。许多指令都不会一定范围内的工作,并在后来,当你需要改变一些东西是如何工作的,你很可能会打破你所拥有的不是简单地修改吧。

最好的办法是先并不使用htaccess的文件:

  

您应该避免使用.htaccess文件完全如果你有机会为httpd主服务器的配置文件。使用.htaccess文件会减慢你的Apache HTTP服务器。任何指令,它可以包括在.htaccess文件是在一个目录块更好的设置,因为这将有更好的表现同样的效果。

创建本地,开发和生产一个独立的虚拟主机。打开它们或根据需要关闭,无论全局配置它们共享,存储,在其他地方(如在一个名为 global.includes ),然后使用包括指令在所有3个虚拟主机。如果您需要规则适用于特定的目录,使用<目录> 块,而不是htaccess的文件

如果你宁愿坚持里面的一切htaccess的文件,你的可以的试图把一切的< IfDefine> 的,它可能是你必须你的伪code在你的问题最接近。从本质上讲是这样的:

 #全球htaccess的规则
RewriteEngine叙述上
重写规则^富$ /条[L]

#只有本地
< IfDefine LocalInstance>
    重写规则^本地/富/酒吧[L]
< / IfDefine>

#只开发
< IfDefine DevInstance>
    重写规则^开发/富/酒吧[L]
< / IfDefine>

#只生产
< IfDefine ProductionInstance>
    重写规则^开发/富/酒吧[L]
< / IfDefine>
 

然后,当你启动Apache,你需要传递 -DLocalInstance -DDevInstance -DProductionInstance 作为命令行paramaeters或使用定义指令(只有一个参数),在你的虚拟主机配置的地方。这是不能保证的那样顺利,因为它看起来像它应该工作中,我遇到了无法解释的问题,< IfDefine> 之前,特别是如果你试图太花哨。

I know similar questions have been asked before but I haven't found any really specific answers for my situation.

I have an ExpressionEngine site running on multiple environments (local, dev, production), and each of those environments needs different .htaccess rules:

All Environments

Development

  • Password Protect with .htpasswd
  • Force HTTPS protocol
  • Prevent search engine indexing with X-Robots-Tag

Production

  • Force HTTPS protocol
  • Redirect non-www subdomains to www

Local

  • No unique rules.

I've seen a lot of examples of how you can set specific environments per-module. Like RewriteCond %{REQUEST_HOST} ^dev.myurl.com for the mod_rewrite module, and tricks like this for .htpasswd requirements.

But what I would really prefer is some way to set global environment variables, then re-use those variables in the .htaccess file per-environment. To use pseudo-javascript as an example, something like:

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
}

This way all of the environment-specific rules are all grouped together, making a really clean file, and only one variable needs to be changed if an environment is changed.

I've seen a few references to altering settings in Apache's config files, but obviously that's not a viable option if I'm dealing with 3rd-party hosts.

So is this pie-in-the-sky wishful thinking, or can it be done?

解决方案

So is this pie-in-the-sky wishful thinking, or can it be done?

IMO, yes. You're never going to be able to get predictable "scoping" of rules based on ENV variables or anything like that. There doesn't exist arbitrary if(something) { do everything in here } in apache. Lots of directives won't work inside certain scopes, and in later on, when you need to change how something works, you're more likely to break what you have than simply amending it.

The best way is to not use htaccess files at all:

You should avoid using .htaccess files completely if you have access to httpd main server config file. Using .htaccess files slows down your Apache http server. Any directive that you can include in a .htaccess file is better set in a Directory block, as it will have the same effect with better performance.

Create a separate vhost for local, dev, and production. Turn them on or off as needed, whatever global config they share, store that elsewhere (like in a file called global.includes) and then use the Include directive in all 3 vhosts. If you need to apply rules to specific directories, use the <Directory> block instead of htaccess files.

If you'd rather stick everything inside htaccess files, you could try putting everything in <IfDefine> blocks, it's probably the closest thing you'll have to your pseudo-code in your question. Essentially something like:

# 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>

Then when you start apache, you'd need to pass in -DLocalInstance, -DDevInstance, or -DProductionInstance as command line paramaeters or using the Define directive (with only one argument) somewhere in your vhost config. This isn't guaranteed to work as smoothly as it looks like it should, I've ran into unexplained issues with <IfDefine> before, especially if you try to get too fancy.

这篇关于的.htaccess在多环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆