.htaccess in Multiple Environments

前端 未结 2 1177
北恋
北恋 2021-02-13 13:21

I know similar questions have been asked before but I haven\'t found any really specific answers for my situation.

I have an ExpressionEngine site running on multiple en

2条回答
  •  不思量自难忘°
    2021-02-13 13:33

    So is this pie-in-the-sky wishful thinking, or can it be done?

    IMO, yes. You're never going to be able to get predictable "scoping" of rules based on ENV variables or anything like that. There doesn't exist arbitrary if(something) { do everything in here } in apache. Lots of directives won't work inside certain scopes, and in later on, when you need to change how something works, you're more likely to break what you have than simply amending it.

    The best way is to not use htaccess files at all:

    You should avoid using .htaccess files completely if you have access to httpd main server config file. Using .htaccess files slows down your Apache http server. Any directive that you can include in a .htaccess file is better set in a Directory block, as it will have the same effect with better performance.

    Create a separate vhost for local, dev, and production. Turn them on or off as needed, whatever global config they share, store that elsewhere (like in a file called global.includes) and then use the Include directive in all 3 vhosts. If you need to apply rules to specific directories, use the block instead of htaccess files.

    If you'd rather stick everything inside htaccess files, you could try putting everything in blocks, it's probably the closest thing you'll have to your pseudo-code in your question. Essentially something like:

    # Global htaccess rules
    RewriteEngine On
    RewriteRule ^foo$ /bar [L]
    
    # Only local
    
        RewriteRule ^local/foo /bar [L]
    
    
    # Only dev
    
        RewriteRule ^dev/foo /bar [L]
    
    
    # Only production
    
        RewriteRule ^dev/foo /bar [L]
    
    

    Then when you start apache, you'd need to pass in -DLocalInstance, -DDevInstance, or -DProductionInstance as command line paramaeters or using the Define directive (with only one argument) somewhere in your vhost config. This isn't guaranteed to work as smoothly as it looks like it should, I've ran into unexplained issues with before, especially if you try to get too fancy.

提交回复
热议问题