Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.htaccess between developemt, staging, and production

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

like image 965
Chuck D Avatar asked Jul 20 '11 15:07

Chuck D


2 Answers

# 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]
like image 97
Dumitru Ceban Avatar answered Dec 16 '22 08:12

Dumitru Ceban


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 ;-)

like image 36
Rudi Visser Avatar answered Dec 16 '22 08:12

Rudi Visser