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:
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
# 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]
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 ;-)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With