.htaccess file on prod and tracked in git, do not want to use prod's .htaccess on dev

Viewed 1312

If this is a duplicate, please mark it is as so. Also, I'm not exactly sure if this would be an opinion-based question. My googling has not returned anything relevant to my predicament, and I'm not sure what I should be googling either. (1, 2)

My question is about how to keep a production .htaccess file that is tracked in git from interfering with my development environment's web server.

A brief description of my git deployment setup:
Local machine (dev) -> Bitbucket -> Production

Production is on a shared host, so I can't directly push to production from dev, I have to use Bitbucket as an intermediary.

My development environment:
I use Vagrant locally as my development environment, which uses Apache as its web server. The vagrantbox setup script applies changes in the httpd.conf file to set up the VirtualHost, DocumentRoot and DirectoryIndex.

This has worked fine for me in the past.

On production, Apache is also used. I originally did not have .htaccess tracked in git, but there was a change recently (an HTTP to HTTPS redirect) that made me decide to start tracking it. The problem I have now is that the production .htaccess file is getting pulled to my dev directory and affecting my dev web server.

A few ideas I had were:

  1. Creating a separate git repo to track the .htaccess file on production that will not be added to Bitbucket or dev, but this feels convoluted to me.
  2. Creating a branch to track the .htaccess file but ... I foresee that getting messy for me because I'll be juggling branches on prod/dev and there's a strong chance I'd screw something up.
  3. I forego tracking the .htaccess file altogether, but that seems like a recipe for disaster later on.

Is there another way to do this? Or should I use one of the ideas I thought of?

3 Answers

The branch aproach could be good idea, maybe a develop branch without the .htaccess files and a production branch like master with the tracking. So when you have something ready for production you do a merge to master.

The other answer is probably the one you are looking for, however, there maybe a couple of alternatives without changing version control...

If your local dev environment does not use .htaccess at all, then you could disable this in your server config, then it wouldn't matter if the prod .htaccess file is present or not:

<Directory /path/to/public_html>
    # Disable .htaccess files...
    AllowOverride none
</Directory>

If you need .htaccess in both prod and dev but with (some) different directives then you can include conditionals in your .htaccess file. There are various ways you can do this, one way is to Define a variable in the server config of your dev server and check for this in .htaccess.

For example, in your server config:

Define DEV

In .htaccess:

<IfDefine DEV>
    # Development server directives...
</IfDefine>

<IfDefine !DEV>
    # Production server directives...
</IfDefine>

Note the ! (not) prefix on the second rule block.

I've run into the exactly same problem ... what I'm currently doing is I keep my working dev directory under /var/www/site [dev]/ but I've gitignored the .htaccess files along with anything else that's only for development. Then I keep a second directory right next to it at /var/www/site [prod]/ with all the variations I want on the production server. So I commit and push from [dev], pull back down to [prod] and deploy from there. I mean, it works - but it feels like there should be an industry standard for stuff like this, which is why I'm here. But whatever, I guess if it works, it works. shrugs

Related