开发、登台和生产之间的 .htaccess
我正在开发一个使用 url 重写并具有特定 .htaccess 配置的应用程序。在开发应用程序时,我遇到三个问题:
- 在本地计算机 (localhost) 上开发
- 登台 (staging.mydomain.com)
- 生产 (www.mydomain.com)
我不断地向登台和生产环境推送新的升级,每次我覆盖现有的源代码我必须去更改.htaccess 文件。有没有办法可以将 .htaccess 通用到目录或让它自动检测其环境?
我当前的 .htaccess 文件如下。我只是取消注释不同环境之间的部分,但很想停止这样做......
# Development
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ /app/index.php?request=$1 [L,QSA]
# Staging
# RewriteEngine on
# RewriteCond %{REQUEST_FILENAME} !-f
# RewriteCond %{REQUEST_FILENAME} !-d
# RewriteCond %{REQUEST_URI} !=/favicon.ico
# RewriteRule ^(.*)$ /html/app/index.php?request=$1 [L,QSA]
# Production
# RewriteEngine on
# RewriteCond %{REQUEST_FILENAME} !-f
# RewriteCond %{REQUEST_FILENAME} !-d
# RewriteCond %{REQUEST_URI} !=/favicon.ico
# RewriteRule ^(.*)$ /index.php?request=$1 [L,QSA]
提前致谢!
查克
I'm working on a app that uses url rewrites and has a specific .htaccess configuration. When working on the app I have three eviorments:
- Developent on my local machine (localhost)
- Staging (staging.mydomain.com)
- Production (www.mydomain.com)
I am constantly pushing new upgrades to the staging and production environment and each time I overwrite the existing source code I have to go in an change the .htaccess file. Is there a way I can the .htaccess generic to the directory or have it automatically detect it's environment?
My current .htaccess file is below. I just un-comment the sections between the different environments but would love to stop doing that...
# Development
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ /app/index.php?request=$1 [L,QSA]
# Staging
# RewriteEngine on
# RewriteCond %{REQUEST_FILENAME} !-f
# RewriteCond %{REQUEST_FILENAME} !-d
# RewriteCond %{REQUEST_URI} !=/favicon.ico
# RewriteRule ^(.*)$ /html/app/index.php?request=$1 [L,QSA]
# Production
# RewriteEngine on
# RewriteCond %{REQUEST_FILENAME} !-f
# RewriteCond %{REQUEST_FILENAME} !-d
# RewriteCond %{REQUEST_URI} !=/favicon.ico
# RewriteRule ^(.*)$ /index.php?request=$1 [L,QSA]
Thanks in advance!
Chuck
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
一种选择是在 httpd.conf(或其他地方)中设置定义环境的环境变量。
例如(在
httpd.conf
中):(在
.htaccess
中)未经测试,但我认为这个概念足以解决任何问题;-)
One option would be to setup environment variables in
httpd.conf
(or elsewhere) that define your environment.For example (in
httpd.conf
):(in
.htaccess
)Untested, but I think the concept is sound enough to figure out any issues ;-)
您是否可以将
.htaccess
文件放在每个环境中,然后忽略您正在使用的任何 FTP 或部署程序中的文件?或者,我所做的就是在本地主机中设置
VirtualHosts
,该主机与生产站点具有相同的域,但带有dev.
前缀。例如,www.example.com
和dev.example.com
。这样,无论环境如何,我始终可以确定根目录是我正在使用的任何主机的顶级目录;我不需要重写我的.htaccess
指令。Can you not just place your
.htaccess
files on each of the environments, and then just ignore the file in whatever FTP or deploy program you're using?Alternative, what I do is set up
VirtualHosts
in my local host that is the same domain as the production site, but with adev.
prefix. For example,www.example.com
anddev.example.com
. That way, I can always be certain that the root is the top-level directory of whatever host I'm using, no matter the environment; and I don't need to re-write my.htaccess
directives.Chuck D,Dumitru Ceban 的变种非常好。但我建议您尝试在代码中添加一些标志——而不是在 .htacces 中。
另外,rudi_visser 的建议很好。但我想在这里补充一点,在理想的系统中我们应该只使用 apache 的 *.conf 并拒绝 .htaccess (只是一个愚蠢的表现)。
所以我建议你在代码中添加一些标志,避免 .htacess (如果可以的话)并切换环境。通过简单的conf值或通过一些逻辑,例如
if locaslhost == host then ...
Chuck D, the variant of Dumitru Ceban is very good. But I suggest you to try also option to have some flag in your code -- not in .htacces.
Also, good suggestion of rudi_visser. But I want to add here, that in the ideal system we should only use apache`s *.conf and decline .htaccess (just a stupid performance).
So I suggest you to have some flag in code, avoid .htacess (if you can) and switch env. by simple conf value or by some logic like
if locaslhost == host then ...