Drupal private directory conditional htaccess? - .htaccess

I want my htaccess to work only when current user has no access to the specific node.
I have a private directory within Files in my drupal 8 project.
I would like to know if something like this (bellow) could work and how.
If (user_has_access) {}
else {somehow disable htaccess}

The best way is to setup an $_ENVs flag during the application startup. This flag will be a variable defined on the server and the you case use it like
<IFDefine {variable}>
{executable code}
</IFDefine>
You need to add this flag in Drupal 8's bootstrap function and then include all your executable .htaccess code within the IFDefine block. So if flag is set or defined the code will execute otherwise not. But still the .htaccess loads and functional. This way you can save this file for other purpose as well.
Hope it might help!

Related

Setting up .htaccess for the first time

Totally lost on how to set up a .htaccess file, bunch of stuff and only been able to redirect and set index.
I have a site https://subdomain.domain.com/views/list.html and I want it to show up as https://subdomain.domain.comIve been able to hide the views/list.html from that main page with DirectoryIndex views/list.html but when i come back to it from within the website it still shows up as subfolder.Also is it possible for other subfolder files to not show up as subfolder but as something else? e.g. https://subdomain.domain.com/views/add.html show up as https://subdomain.domain.com/addproduct
Have you thought about trying PHP indexing? Make a folder structure and place the indexer in the correct folder. As for the subbing, it should be possible, least from what I recall.

Include another htaccess file from .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.
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.
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.
It is possible - in 2-3 steps...
Create your file of IPs to Deny. It could be a .php file, .txt file, even .csv file.
Create a .php (or language of your choice) script, which purpose is to output a file named ".htaccess".
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.)
You can create server side script like PHP ".htaccess.php" and inside put your includes. Of course, you must configure apache:
AccessFileName .htaccess.php

.htaccess or symbolic link (symlink)

I have a website with multiple folders and I was trying to fix them in my .htaccess. After a little while, I have a big .htaccess with rules that conflicts.
Now every time I want to add a folder I have to add it to the .htaccess.
I did some research and I found out I can create symbolic link instead, so no more .htaccess
In both solution I have to create or modify something so for me its the same result at the end but is it a better practice to create instead symbolic link ?
Symbolic links are faster yes (like Aki said) but here's my thoughts on this.
if you have images, css or js files then you don't need to rewrite or create symbolic links. You can use the full URL (eg /images/...) or use a common domain like i.domain.com (or anything you want) and refer all your JS, Images and CSS there. Eg: i.domain.com/logo.jpg or js.domain.com/site.js.
This way, you never have to think about rewriting rules or create links you might forget one day.
This one is very easy to manage and maintain if you need to add images, change js or update your CSS since you only have one point of entry and automatically everything be updated.
use symblink, .htaccess has to be proccesed by apache whereas the symblink are proccess by the OS which is faster.
creating 100 rules vs 100 symblink, if the rule you looking for is at the last you will have to parse all of them then use the one you need.

How to map an absolute URI Path from "/xxx/some.file" to "/something/xxx/some.file" using .htaccesss

thanks for your help; I'm new to all this URL mapping, however I believe this is a simple problem...
I'm migrating a ColdFusion application from GoDaddy (who recently dropped their ColdFusion support) to CFDynamics.
I have a URL for my new root: cfd123.cfdynamics.com/lbc this is my wwwroot directory where I've put the website. (It's actually: D:\Inetpub\wwwroot\lbc.com\wwwroot but I don't think that matters)
The problem is that all the HTML URIs: href and src attributes, etc. within the site were written using absolute paths: "/images/some.jpg", etc. and now I want them to map to "cfd123.cfdynamics.com/lbc/images/some.jpg".
I figure I need to map "/something" to "/lbc/something" everywhere it's referenced. This seems like it should be simple, just map a leading "/" to "/lbc/" and I'm good to go.
So, I went to create an "Alias / /lbc/" sort of listing and put it in my .htaccess file. Now I discover that Alias isn't allowed in .htaccess. (I think if I had access to httpd.conf I could use an Alias there, but it's a shared hosting environment and I don't have that access.)
So, I thought I'd try rewriting rules (still in the .htaccess file):
RewriteEngine on
RewriteBase /lbc
RewriteRule ^/(.*)$ /$1 [L,R=301]
To no avail. So, I'm not finding that any of the obvious things work (and I'm not even sure I've done them correctly). I don't see any debug mechanisms that are showing me anything (chrome just shows me the 404 errors one would expect when a file isn't found.)
And this is all new to me and starts getting pretty hairy pretty fast. Can anybody point me in the right direction?
Thanks, Steve
p.s. I can take care of the ColdFusion CFInclude directives by creating an Application.cfc file in the wwwroot directory containing the code below, but that doesn't do anything for the various HTML links throughout the page.
<cfset this.name = "AbsoluteReference"/>
<cfset this.mappings = structNew() />
<cfset this.mappings["/"] = getDirectoryFromPath(getCurrentTemplatePath())/>
Your running a windows server at CFDynamics I can tell from this
I have a URL for my new root: cfd123.cfdynamics.com/lbc this is my wwwroot directory where I've put the website. (It's actually: D:\Inetpub\wwwroot\lbc.com\wwwroot but I don't think that matters)
This is a windows structure "D:\Inetpub\wwwroot\lbc.com\wwwroot" so .htaccess will not work for URL rewrites, since your on a shared environment you will not be able to do what you need to. These changes would have to be made in IIS and that's not an option. If you have Dreamweaver you can do a mass find replace for /images/ and replace with /ibc/images/ and for link do href="/ replace with href="/ibc/.
Hope this helps.

How to specify named subfolders in htaccess?

I have a folder structure like this /img/products/{product name}/ and then the sub folders hi, low, and thumb.
I want to use htacess to force-download any files in a 'hi' or 'low' subfolder (but not 'thumb').
I was hoping something like this would work:
<FilesMatch "\(.*)(\/hi|\/low)(.*)">
ForceType applicaton/octet-stream
</FilesMatch>
Now I'm not great with regex, but that seems to work in regex testers against paths like
/img/products/active/low/something.jpg
However it's not working on the site.
Any suggestions?
Thanks
Pete
This probably should have been a ServerFault question based on what I think that you're trying to do, but since you actually can't do what you're trying to do (the way I think you're trying to do it), I'll provide two alternatives; one that likely won't work, and another that involves a PHP script (which should hopefully be alright for you, since your question history shows you asking something about PHP before).
The Problem:
First, what I think you're trying to do, so you can correct me if I'm wrong:
# Apply ForceType to anything that's in a path that matches this
<FilesMatch "img/products/[^/]+/(hi|low)/[^/]+$">
ForceType applicaton/octet-stream
</FilesMatch>
However, this won't work, because FilesMatch only examines the filename, under the assumption that you could either appropriately place the .htaccess file, or combine the directive with a Directory statement in the server or virtual server configuration.
In your case though, this isn't possible (Well, I assume anyway, maybe you do have access to the necessary configurations, but since your question is tagged .htaccess I'm guessing probably not), given that copying a .htaccess file to every folder isn't realistic.
The Solutions:
As it turns out, mod_rewrite, along with performing all sorts of voodoo in the way of filename resolution, also gives you extensions of other Apache functionality that you would not necessarily have been able to use otherwise. Case in point, we can set the MIME type using the T flag, making the easiest solution this:
RewriteEngine On
# Force the MIME-type, but don't actually perform a rewrite
RewriteRule ^img/products/[^/]+/(hi|low)/[^/]+$ - [T=application/octet-stream]
This actually works pretty well, but chances are good that your Apache installation thinks that it knows better than you, and includes a mimes.types file in the main configuration that maps the jpg extension to image/jpeg. This value takes precedence over the RewriteRule, making it ineffective in that case.
The other solution is to create a small script that acts as the go-between, passing the appropriate headers and image data from the server to the client. You would then use mod_rewrite to pass the request on to that script:
RewriteEngine On
# For an added bit of sanity, make the test pattern even more restrictive
RewriteRule ^img/products/[A-Za-z._-]+/(hi|low)/[A-Za-z._-]\.[A-Za-z]+$ imageDownloader.php
As for the script itself, to keep this answer from getting ridiculously long, I suggest taking a look at this answer or one of the other questions on this topic, keeping in mind that it's imperative that you screen the filenames that can be downloaded for reasons of security. Note that you would be able to get the original request path from $_SERVER['REQUEST_URI'], and could use that to locate the proper image.

Resources