Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include another htaccess file from .htaccess

Tags:

.htaccess

Is it possible to do include rules from another htaccess file in .htaccess ?

.htaccess

RewriteEngine On
RewriteCond ...
RewriteRule ...

Include .htaccess1
.
.
Include .htaccess2

Doing this gives a 500. Include not allowed here Is there a way to do this ? Because I need this pretty badly.

like image 848
EPQRS Avatar asked Nov 30 '12 07:11

EPQRS


People also ask

Can .htaccess include another file?

htaccess file can't "include()" other files, but a . php script can, and . php scripts can output files named ". htaccess", that can be built by script.

Where is .htaccess file located?

htaccess file is located in the root directory of your WordPress site. Depending on your hosting provider, the root directory may be a folder labelled public_html, www, htdocs, or httpdocs. You can locate it by using File Manager in your hosting account's cpanel.

Where is my .htaccess file Apache?

htaccess file can be found at installdir/APPNAME/. htaccess. Some applications do not have the installdir/apache2/conf/vhosts/htaccess/APPNAME-htaccess.


3 Answers

You can't include rules, statements, definitions, or directives from other files from an htaccess file. The Include directive can't be used inside an htaccess file. Part of the point of an htaccess file is to act similarly to a <Directory> block but be self contained and unable to access things outside of the directory itself (subdirectories are ok) but more specifically nothing outside of the document root. This way, someone doing malicious things won't be able to point requests or include files/content from other directories by hacking the htaccess file.

In the scope of mod_rewrite specifically, there are options for the RewriteOptions that allow inheriting rewrite rules from the htaccess file from a parent directory, but nothing to arbitrarily include rules from anywhere.

like image 110
Jon Lin Avatar answered Oct 17 '22 15:10

Jon Lin


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.

like image 25
FSG Avatar answered Oct 17 '22 15:10

FSG


It is possible - in 2-3 steps...

  1. Create your file of IPs to Deny. It could be a .php file, .txt file, even .csv file.

  2. Create a .php (or language of your choice) script, which purpose is to output a file named ".htaccess".

  3. Every time you update your file of IPs to Deny, run the said 2) .php script, and output a new .htaccess, to each of your Domains.

If you have statements in addition to IPs to Deny, hard code them in your .php script to output first. See https://www.askapache.com/htaccess/

The .php script could generically look like:

$output = "statement1".PHP_EOL;
$output .= "statement2".PHP_EOL;
$output .= "statement3".PHP_EOL;
...

Then, when you're ready for the DENY portion:

$denyList = file_get_contents("the/list/of/IPs.txt"); // or .php etc.
$ArrayDenyList = explode(PHP_EOL,$denyList); // or your line ending character, if necessary
foreach($ArrayDenyList as $key =>$value) {
$output .= 'Deny from '.$value.PHP_EOL;
  }

Then write the file: (you probably have a standard way):

$handle = fopen(.htaccess,"w"); //complete path if in another domain
fwrite($handle,$output);
fclose($handle);
echo "Success - <p>";

If you have more than one Domain, then have an Array of those Domain name's path, and foreach that Array, and fwrite to each path.

If some Domains already have .htaccess requirements, put that in a readable .txt file... do a file_get_contents(on that), "output =" that, add the "Deny's, then "output .= ..." each in a "foreach()" loop.

Anyway... you've done all these things before... just apply each technique to this scenario.

An .htaccess file can't "include()" other files, but a .php script can, and .php scripts can output files named ".htaccess", that can be built by script.

The above method works, and I started doing it when I needed to create rows of "RewriteRule" for each of my catalog of products! Every time I add a new product, I run my script that outputs a fresh .htaccess file... built from my SQL table of products. Similar to my .php script that "fwrite"s my sitemap.xml.

(I hope no .php punctuation or "$"s got lost in this typing.)

like image 4
Nick Avatar answered Oct 17 '22 14:10

Nick