开发、分期和生产之间的 .htaccess

2022-08-30 22:01:37

我正在开发一个使用url重写并具有特定.htaccess配置的应用程序。在应用程序上工作时,我有三个环境:

  1. 在我的本地计算机上开发(本地主机)
  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]

提前致谢!

卡盘


答案 1
# 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]

答案 2

一种选择是在定义环境的(或其他地方)设置环境变量。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]

未经测试,但我认为这个概念足够合理,可以找出任何问题;-)


推荐