htaccess serve images from Amazon AWS - .htaccess

I can't get this to work.
I need to use htaccess to serve images on AWS as if they were on my website.
Example: http://www.example1.com/images/meme.jpg
should show the image from
https://bucket1.s3-us-west-1.amazonaws.com/meme.jpg
But it's NOT a redirect. The address is still shown as http://www.example1.com/images/meme.jpg in the browser
A colleague is using this:
RewriteEngine On
RewriteRule ^images/(.*)$ http://bucket1.s3-us-west-1.amazonaws.com/$1 [P]
HOWEVER when I do that I get this:
This XML file does not appear to have any style information associated with it. The document tree is shown below.
<Error>
<Code>NoSuchBucket</Code>
<Message>The specified bucket does not exist</Message>
<BucketName>www.example1.com</BucketName>
<RequestId>4CE66A21A22EC3CA</RequestId>
<HostId>
BlAEYrdEO+xm4fGSwjyeZX/pn7IVfHPNGiLJpWRta/PNxekFnLuXHnbX2EGjkIdY2gVM65E2ymI=
</HostId>
</Error>
Why does it seem to be putting my domain as the bucket name??? Ripping my hair out for this one.

Related

Trying to obtain images from another location but RedirectMatch in htaccess is ignored

I've just setup a new subdomain 'preview', ie preview.example.com and I am setting up a replicate of the codebase of a live website (from the www. subdomain) so that I can have a duplicate environment for testing and showing my client recent changes.
I want to save myself having to copy over all the images to the preview and so the normal location under which images are saved in a WordPress site /wp-content/uploads/ is empty. I wanted to ues a simple htaccess rule to get these instead from the live subdomain where the image at the same path will exist. In other words, a request for
https://preview.example.com/wp-content/uploads/2021/01/test.jpg
would be redirected to the following where the image exists..
https://www.example.com/wp-content/uploads/2021/01/test.jpg
I've tried to set this up with the simplest of htaccess redirect rules but it just seems to be ignored and I've no idea why. Any idea how I can troubleshoot this?
RedirectMatch 301 /wp-content/uploads/(.*) http://www.example.com/wp-content/uploads/$1
Converting my comment to answer so that solution is easy to find for future visitors.
Scenario is that both subdomain and main domains are on same host but their DocumentRoot are set to different paths. OP wants to serve lot of images from subdomain but doesn't to do bulk copy.
Suggested approach without tinkering with .htaccess is to create a symbolic link of wp-content/uploads/. Only option suggested to use in .htaccess is:
Options +FollowSymLinks
Which allows use of symbolic links for serving web requests.

How do I block a specific image using .htaccess?

Is there any way to deny access to a specific image in .htaccess alone? If I were to block and image such as "https://www.w3schools.com/tags/smiley.gif" how would I format it?
I would also like to add that I don't own said image, I'm trying to block an off-site image in .htaccess.
Using an Apache Expression (Apache 2.4+):
<If "%{REQUEST_URI} == '/tags/smiley.gif'">
Require all denied
</If>
UPDATE: I initially thought your reference to w3schools.com was just a misleading example, but if this is really an "off-site image", as stated in your updated question, then you can't block a request to externalsite.example in the .htaccess file at example.com. Your example.com/.htaccess file naturally only processes requests that target example.com.
You need to modify the HTML source so that the link to the external site doesn't appear on the page in the first place.
No, you can't on a server block an image on another server.
If you control the HTML that requests the image you can block it in your HTML code - but .htaccess has nothing to do with it.

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.

Simple redirect rules for Amazon S3

I'm using S3 and CloudFront to store the images, CSS and JS files of my web site - which is not static and is hosted on a proper web server
Since the CSS file changes frequently, I'm using a version number to make sure the user browser reloads it when it changes. When I was hosting the CSS file on my Apache web server, I was using the following redirect rule
RewriteEngine On
# CSS Redirection (whatever.min.5676.css is redirected to whatever.min.css)
RewriteRule ^(.*)\.min\.[0-9]+\.css$ $1.min.css
With this simple rule, http://www.example.com/all.min.15.css redirected to http://www.example.com/all.min.css
How can I reproduce such a rule with Amazon S3 and/or CloudFront ?
i.e. to have http://example.amazonaws.com/mybucket/css/all.min.3.css or http://example.amazonaws.com/mybucket/css/all.min.42.css redirected to http://example.amazonaws.com/mybucket/css/all.min.css
(Note : my S3 bucket is NOT configured as a website but should it be so to enable redirection rules?)
NOTE: this answer does not use any rule. It might not be the proper answer.
I would be using a query parameter to handle different versions, like:
http://example.amazonaws.com/mybucket/css/all.min.css?ver42
http://example.amazonaws.com/mybucket/css/all.min.css?42
http://example.amazonaws.com/mybucket/css/all.min.css?ver=42
http://example.amazonaws.com/mybucket/css/all.min.css?20141014
To be exact, in my dynamic web page, the version parameter is stored in a variable and appended to url (both CSS and JS). While in development I only have to increase/set one variable to force the browser to load a new version. This way, there is no need for rewrite rules, even on Apache.
Caching also works as the Last-Modified and ETag headers are kept in tact.
Hope this helps.

URL rewrite rule in the same file (using anchor-lists)

I´ve just made a website based upon an only index.html file. You can surf the file using the menu that is made of anchored lists. The problem is that the URL is the name of the anchor, so I have two options:
1.- Rename the whole anchored lists (but there would be still some problems)
2.- Rename the URL using .htaccess, doing RewriteEngine On.
The URL you can see when access to the homepage is:
http://domain.com/#!/page_home
Notice: Only 'domain.com' is not real.
And next 'pages' are then:
http://domain.com/#!/page_2
http://domain.com/#!/page_3
http://domain.com/#!/page_4 and so on...
And I want to be displayed http://domain.com/welcome instead http://domain.com/#!/page_home.
Well, I´ve follow some basic and advanced tutorials with no luck. In theory using something like this should work:
RewriteEngine On
RewriteRule ^#!/page_home welcome
But this and others combinations didn´t work for me.
.htaccess is working because I have another rules like this one:
Options -Indexes
ErrorDocument 404 /notfound.html
Can you give me a hint please?
Thanks in advance.
I'm sorry to tell you this, but it won't work.
As you know already, anchor is a link to an internal resource in a web page. When you click on an anchor, no request is sent to the server, only the browser goes to the requested resource in the same page. If no request is sent to the server, then no htaccess is called and no redirections are made.
Your only option if you want the links to be like that is to redo the website without using anchors ...

Resources