Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

I want to have the ability to test .htaccess rules before saving them to the main .htaccess file.

Is it possible to set a specific request header and have Apache use a set of rules from a different .htaccess file.

share|improve this question
    
Yes, I know that you can/should use development sites/environments for testing these types of changes, but am asking because the thought occurred to me that I might be able to do this as well. Using ".htaccess" inside quotes because the name of the secondary file name would obviously not be able to be .htaccess –  Dez Jun 12 at 19:31
    
Start with this guide –  eyoung100 Jun 12 at 19:58

1 Answer 1

This may be possible with Apache 2.4's new features which includes <If>, <ElseIf> and <Else> directives.

http://httpd.apache.org/docs/2.4/mod/core.html#if

Possible example:

AccessFileName .htaccess
<If "$req{Host} != 'test.mydomain.com'">
    AccessFileName .htaccess_test
</If>

For older versions I am not aware of any such options. Perhaps using SetEnvIf would work.

http://httpd.apache.org/docs/2.2/mod/mod_setenvif.html

SetEnvIf Remote_Host "^test\.mydomain\.com$" htaccess_file_name=.htaccess_test
SetEnvIf Remote_Host "^www\.mydomain\.com$" htaccess_file_name=.htaccess
AccessFileName htaccess_file_name

I haven't tried this so I can't say that it will actually work at all but it's worth a shot.

share|improve this answer
    
I don't think you'll be able to change the AccessFileName per request since this directive needs to go in httpd.conf (or a virtual host context) and is therefore not executed on each request. However, you could perhaps execute different sections within the same .htaccess file, depending on the request (using <if> on Apache 2.4 - as you suggest), or perhaps <IfDefine> on earlier versions of Apache? –  w3d Jun 12 at 21:26
1  
Yes this would require editing the httpd.conf or related virtual host config. When I have a chance I will test this out to be sure. I don't think it is possible with any versions prior to 2.4 though. I do know that using SetEnvIf variables wont work with <IfDefine>, unfortunately. –  Dave Lozier Jun 12 at 22:04
    
"is therefore not executed on each request" - Sorry, I'm not sure what I was thinking there, that is obviously not correct. However, I was just trying to have a go with this myself but (for some reason) I'm unable to set AccessFileName conditionally inside an <If> block? I'm simply unable to restart the server (unhelpful error). I can set AccessFileName outside of the <If> block and use other directives inside the <If> block, but not AccessFileName? Did you manage to test this? –  w3d Jun 13 at 11:35
    
Sorry I wasn't able to get back and post a reply. I had the same problem with apache complaining that AccessFileName couldn't be used inside the <If> block. I think the only viable solution is to create a testing virtual host domain that is pointed at the same document root and set the AccessFileName to something else in that vhost configuration. –  Dave Lozier Jun 15 at 3:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.