Forum Topic: Can I use includes in .htaccess?

Forum: .htaccess Forum : General • Posted by Leon Fernandez • Updated:

What I mean here is if it is possible to merge other files into a master file like the include directive in php, and if so, how could it be done (calling externals)??

For example, I could have a file called .htmaintenance (for site maintenance), .htsecurity (for security directives, for example Gseries blacklists), .htperformance (for compression and all), .htseo (for rewrites) and call them into a master file called (you guessed it) .htaccess, and just comment (#) or uncomment when needed.

This would consequently have it more ordered and easily maintained when needed, and obviously easier to detect problems and or use directives when needed simply by (un)commenting when necesary.

Might sound strange but I think I read it somewhere, although I cannot find it anymore. Or maybe I'm shortcircuiting somehow by reading so much about apache, linux, php and others (I think I need to go to the beach without a cell, tab nor reading facilities to fresh my mind up a bit, haha).

3 Replies to “Can I use includes in .htaccess?”

Jeff Starr
Posted by Jeff Starr

It may be possible, but I have not encountered anything yet on the topic. If you do find anything please post with a follow-up, thanks!

Leon Fernandez
Posted by Leon Fernandez •

Still cant find anything specific, although I did find an interesting answer (answer 2) from here:

http://stackoverflow.com/questions/13640511/include-another-htaccess-file-from-htaccess

Where it says:
I know this may be a little late, but instead if you are attempting to implement an IP ban, or similar type of dynamic rule content, use the very last rule in your .htaccess file to point the request at a single .php or similar languaged script that performs this function (your single script can load the dynamic rules in your own format and make that same decision as Apache would have depending on what your actually trying to accomplish) then passes the request on to the actual page being requested. It adds a layer to the whole request processing, but gives the same dynamic function to all pages without the need to generate direct rules for the server and attempting to have Apache make the decisions for you.

Just thought I would add this thought for anyone else who may stumble across this post looking for something similar.

=========================

Which seams that he is trying to say that you can make a php file with all goodies inside and force all pages to pass thru the rules in that specific files when rendering, although I have no idea how.

Jeff Starr
Posted by Jeff Starr

Yes that is a common approach, and very useful as well. Basically there is no mystery to it, you just do something like this:

<IfModule mod_rewrite.c>
	RewriteCond %{THE_REQUEST} ^/whatever [NC]
	RewriteRule .* /process.php [L]
</IfModule>

So after matching whichever criteria, you just redirect to the PHP script instead of a URL. Then in the PHP file the sky is the limit and you can do some amazing things with your traffic, etc.