.htaccess between developemt, staging, and production

前端 未结 4 2074
予麋鹿
予麋鹿 2020-12-28 17:55

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 loca
相关标签:
4条回答
  • 2020-12-28 18:11

    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.

    0 讨论(0)
  • 2020-12-28 18:15
    # 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]
    
    0 讨论(0)
  • 2020-12-28 18:23

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

    0 讨论(0)
  • 2020-12-28 18:27

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

    0 讨论(0)
提交回复
热议问题