开发、登台和生产之间的 .htaccess

发布于 2024-11-25 09:54:50 字数 993 浏览 3 评论 0原文

我正在开发一个使用 url 重写并具有特定 .htaccess 配置的应用程序。在开发应用程序时,我遇到三个问题:

  1. 在本地计算机 (localhost) 上开发
  2. 登台 (staging.mydomain.com)
  3. 生产 (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:

  1. Developent on my local machine (localhost)
  2. Staging (staging.mydomain.com)
  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

無處可尋 2024-12-02 09:54:50
# Development

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteCond %{HTTP_HOST} ^localhost
RewriteRule ^(.*)$ /app/index.php?request=$1 [L,QSA]

# Staging

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteCond %{HTTP_HOST} ^staging.mydomain.com
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
RewriteCond %{HTTP_HOST} ^www.mydomain.com
RewriteRule ^(.*)$ /index.php?request=$1 [L,QSA]
# Development

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteCond %{HTTP_HOST} ^localhost
RewriteRule ^(.*)$ /app/index.php?request=$1 [L,QSA]

# Staging

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteCond %{HTTP_HOST} ^staging.mydomain.com
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
RewriteCond %{HTTP_HOST} ^www.mydomain.com
RewriteRule ^(.*)$ /index.php?request=$1 [L,QSA]
饭团 2024-12-02 09:54:50

一种选择是在 httpd.conf(或其他地方)中设置定义环境的环境变量。

例如(在 httpd.conf 中):(

SetEnv ENVIRONMENT production

.htaccess 中)

RewriteEngine on

# Development
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} != /favicon.ico
RewriteCond %{ENV:ENVIRONMENT} = development
RewriteRule ^(.*)$ /app/index.php?request=$1 [L,QSA]

# Staging
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} != /favicon.ico
RewriteCond %{ENV:ENVIRONMENT} = staging
RewriteRule ^(.*)$ /html/app/index.php?request=$1 [L,QSA]

# Production
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} != /favicon.ico
RewriteCond %{ENV:ENVIRONMENT} = production
RewriteRule ^(.*)$ /index.php?request=$1 [L,QSA]

未经测试,但我认为这个概念足以解决任何问题;-)

One option would be to setup environment variables in httpd.conf (or elsewhere) that define your environment.

For example (in httpd.conf):

SetEnv ENVIRONMENT production

(in .htaccess)

RewriteEngine on

# Development
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} != /favicon.ico
RewriteCond %{ENV:ENVIRONMENT} = development
RewriteRule ^(.*)$ /app/index.php?request=$1 [L,QSA]

# Staging
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} != /favicon.ico
RewriteCond %{ENV:ENVIRONMENT} = staging
RewriteRule ^(.*)$ /html/app/index.php?request=$1 [L,QSA]

# Production
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} != /favicon.ico
RewriteCond %{ENV:ENVIRONMENT} = production
RewriteRule ^(.*)$ /index.php?request=$1 [L,QSA]

Untested, but I think the concept is sound enough to figure out any issues ;-)

一江春梦 2024-12-02 09:54:50

您是否可以将 .htaccess 文件放在每个环境中,然后忽略您正在使用的任何 FTP 或部署程序中的文件?

或者,我所做的就是在本地主机中设置 VirtualHosts,该主机与生产站点具有相同的域,但带有 dev. 前缀。例如,www.example.comdev.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 a dev. prefix. For example, www.example.com and dev.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.

北方的巷 2024-12-02 09:54:50

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文