htaccess: force download on parameter - .htaccess

My problem is that i want to force image files of every type and documents to automaticly download on click of the specific link.
You can use the attribute download but that does not work on every Browser so i need to send a different Request-Header.
I can't just define a specific URL for that, because that can change really quick and I'm using a CMS so the user likes to select manualy which files should be affected.
My solution was to add an Parameter like:
<a href="/some_folder_structure/some_image_here.jpg?download"
and check via htaccess if the Parameter download is there and redirect with forcing a download.
That's my Code:
RewriteCond %{QUERY_STRING} download
RewriteRule ^(.*) - [T=application/octet-stream]
Can anybody of you see why it doesn't work?
Or should I try something else?

Related

Apache .htaccess redirect to an anchor

I'm trying to do a one-off damage-limitation redirection to an anchor on a page on a website. A wrong URL got published in some publicity material, like this:
https://mydomain.org.uk/A/B
when what I really wanted to publish was
https://mydomain.org.uk/A#B
Having looked at some other answers it seems that any redirect with an anchor needs to be an absolute URL. So I put this in my .htaccess:
RewriteRule A/B https://mydomain.org.uk/A.php#B [NE,L]
(note, the .php is correct, A.php is the page file). And it just simply doesn't work. The browser simply loads A.php and displays it from the top.
I know that the rule pattern is matching, because if I make the target be a completely nonexistent page I get a 404 as expected.
Unfortunately my web hosting service doesn't let me use the Apache log, so it's hard to trace what's going wrong. Can anyone guide me to how to do the rewrite properly so that I pass the #anchor all the way through to the user's browser?
Thanks in advance!
When the RewriteRule is processed by the server, it basically changes internally which resource to access, without the browser noticing.
The only way to change the URL in the browser is to use the redirect flag. This will make the webserver send a HTTP 302 response with a Location header, which then will result in the browser changing the URL and requesting the new page. This new URL can contain an anchor.
In your case the following rule should work:
RewriteRule A/B https://mydomain.org.uk/A.php#B [NE,R,L]
Please keep in mind that anchors are a browser feature so they are normally not sent to the server and therefore neither appear in access logs nor can be used in a RewriteRule.

From HTML to PHP, referencing

I have a website already in place with only html pages that all well referenced. I wanted to improve it and add some php.
But the page is now named index.php.
So I could use something like this in my .htaccess
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)\.htm$ $1.php [nc]
By doing this will I lose the referencing of my html pages by google ? Do I go back to the very begining ?
What will happen if people search for index.html ?
Thank you in advance for your help !
Say google to update it's data, I see that in wordpress exists option that say google when you and new page
Make in htaccsss possibility to find the same page with .htm and .php.
Google with time may be correct the link in his search result to your site to .html.
Make and send new site map to google robot
https://www.google.com/webmasters/tools/submit-url?hl=ru.

hide image path possible with htaccess?

I got image path like that http://blablablabla.com/Admin/img/blablabla.jpg but i do not want to show this path user can i hide this URL with htaccess ?
For example : user will see path like that http://blablablabla.com/Info/img/blablabla.jpg but i have not Info folder this is possible ? so i do not want to see users never see my images paths is way is useful or not i'm not sure tell your ideas which way to most secure to hide my path
Rewriting your url will not make a difference security-wise. If the only security you have is hoping that no-one will ever know that a certain folder exists, then you have no security at all.
The requested rewrite can be done by placing the following directives in a .htaccess file in the www-root of your website.
RewriteEngine on
RewriteRule ^Info/img/([^/]+)$ Admin/img/$1 [L]
This is an internal rewrite that rewrites the url that is requested to an internal url that points to an actual file. $1 is replaced by the first capture group. See the documentation for more information.

htaccess auto redirect while attemp to view direct content

I am currently administrating some art website that contains lots of photos and other content files and it bugs me that ppl find a way around scripting and are accessing stuff directly, they download our copyright protected materials.
I was thinking about htaccess file that do the following:
someone type in address directly to the browser: http://www.mydomain.com/photos/photo.jpg
htaccess triggers and instead of showing the content - it redirects right away to: http://www.mydomain.com/ (this is important to do redirect before picture is displayed)
redirect is extremely important not just some preventing without redirect, but if someone attempts to use sowftware to download content via providing link to it then it rejects request
my knowledge about htaccess is really thin i could use a help on this one
This should work:
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^http://www.mydomain.com/ [NC]
RewriteRule .*\.jpg|gif$ /nolinking.html [R]
If you try enter http://www.mydomain.com/photos/photo.jpg it will redirect you to http://www.mydomain.com/nolinking.html, but it will allow images to be loaded on pages if they are linked to,

Rewrite all requests from a single directory to a script - .htaccess

I need change files url in one folder in my server. It should prevent from direct download but I don't need deny from all rule.
Example:
typical direct link:
http://www.domain.com/foder1/folder2/file.ext
and I need this:
http://www.domain.com/page.html?file=foder1/folder2/file.ext
so in my case I need add this string (page.html?file=) to url. Maybe I'm wrong But I think it must be redirect becouse rewrite not execute page.html just only change (view of) url.
To do this, you need to use mode_rewrite. It will execute the page.html file just fine. It just won't update the URL in the browser bar. So, the user will have no idea they have been directed from the actual file to a script/html page. (As they probably should not!)
Here is an example:
RewriteEngine On
RewriteRule !^foder1/folder2(/|$) page.html?file=%{REQUEST_URI} [L]

Resources