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?
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.
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.
I would opt to not track .htaccess
itself.
Possible options:
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.
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.
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
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.
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