Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need different .htaccess for staging and live site from Git repo

Here's the scenario:

There is a Git repo, which is tracking website files, including the .htaccess at root. The Git repo is checked out to a staging site and to a production site.

The problem: The staging site has to be protected with password (through htpasswd authentication). This changes .htaccess file as well. Now committing or pushing from staging environment means the .htaccess file (which enables password-protection for root directory) is checked-out to the live website as well, making the live website ask for password as well.

Now how shall I go about it? Should I stop tracking .htaccess? Should I overwrite .htaccess through post-receive hook (possible? how?)? Is there any other way to protect a directory without modifying .htaccess? Any other solution?

like image 524
Sawant Avatar asked Nov 17 '12 13:11

Sawant


People also ask

Can you have multiple .htaccess files?

You can have more than one . htaccess file on your hosting account, but each directory or folder can only have one. For example, you can have separate . htaccess files in your root folder and another in a sub-folder.

Should htaccess be in git?

htaccess files in the git repo, but because the live server uses passwords and our local boxes don't, I said we should put them in the . gitignore. However, he says that if you have to have different . htaccess files across instances, you're doing it wrong.


3 Answers

I would opt to not track .htaccess itself.

Possible options:

  1. Track .htaccess-prod and .htaccess-stage (more useful if there are more differences, like various config settings, passwords etc., if it's only one line, then perhaps it's not the best option). Then whenever you update something, you must do it in both files, also you'll have to remember copy the files in two environments as .htaccess manually when they change.

  2. Store only one file .htaccess-example, add hooks in both staging and live areas (look at post-checkout and/or post-merge probably [1]) that will, in both of them, copy .htaccess-example as a local .htaccess whenever there was a change, and additionally, append a line with password protection in staging.

[1] haven't used them myself yet, so I'm a bit confused looking at the documentation. Regarding post-receive, as far as I understand, it is executed on a remote repo when pushing to it, not in the working copy when you pull.

like image 98
jakub.g Avatar answered Oct 21 '22 13:10

jakub.g


In case of Git (contrary to Mercurial) you have only one solution: Branching

You have to use different branches for Staging and Live with single difference in content: .htaccess data. Not bullet-proof, with a lot of handwork, but - it's your choice

like image 41
Lazy Badger Avatar answered Oct 21 '22 13:10

Lazy Badger


Here's an outside the box answer: Set a custom filename for .htaccess file on Dev and don't check it in. For example, .htaccess-dev or .htaccess.staging.

You can set this in the vhosts configuration.

http://www.electrictoolbox.com/change-htacces-filename-apache/

If you use MAMP Pro you can set vhost config per local domain.

like image 22
user137439 Avatar answered Oct 21 '22 13:10

user137439