Search code examples
php.htaccessdevelopment-environment

.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


Solution

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