.htaccess in multiple WordPress environments
Dan Eden recently wrote a great post called changing my ways in which he discusses the recent changes in his WordPress development workflow: moving away from FTP and live editing on a server to Git.
It’s a process I’ve been through recently too. Working in Git has many benefits (which I won’t go into here) but because the same code base is deployed on multiple environments, it can throw up some issues.
One of those issues is how to handle .htaccess on multiple environments. I usually have 3 environments—local, staging and production—and quite often these environments have different subfolders.
Below are two techniques I use.
Using one htaccess file
This is my favourite solution which works 90% of the time. In the example below, I have 3 environments:
- My local environment: localhost/plausiblethought/
- My staging environment: staging.plausiblethought.co.uk/ plausiblethought/
- My production environment: plausiblethought.co.uk
Simply create a .htaccess
file with the following code:
It’s a clever snippet of code which works by setting a variable called rwbase
depending on the URL. First, it checks to see if it is on ‘localhost’ and adds the folder ‘plausiblethought’. It does the same for ‘staging.plausiblethought.co.uk’. If your site is in the root of your staging domain, you can just delete the relevant lines. We don’t need to add anything for the production environment because it’s not in a subfolder.
Using multiple htaccess files and symbolic links
This solution is a bit more fussy and requires SSH access to your server.
First, delete the .htaccess
file (if there is one). Then create a htaccess file for each environment and name it accordingly e.g. .htaccess-local, .htaccess-staging and .htaccess-production. You’ll need to use the default htaccess code and add the relevant folder paths.
Next is the fun bit where we create a symbolic link. A symbolic link is a fancy term for creating a reference to another file. To do this, open Terminal and cd to the correct directory. Then type the following:
ln -s .htaccess-local .htaccess
ln -s
is the magic we need to create the symbolic link. .htaccess-local
is the file we created above and .htaccess
is the file it creates. You should now see a .htaccess
file on your local environment with the same contents as .htaccess-local
.
You’ll then need to repeat this process using SSH on your remote server. Again, cd to the correct directory and then type:
ln -s .htaccess-production .htaccess
That’s it.
I’d love to hear your thoughts on how you work with WordPress on multiple environments. I’m on Twitter: @marcjenkins.