Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have the following URL, I would like to redirect all css, images & javascript to my amazon s3 bucket.

# I have these URL's (and more)
https://secure.mydomain.co.uk/styles/donate/style.css
https://secure.mydomain.co.uk/js/donate/jquery.min.js
https://secure.mydomain.co.uk/images/donate/help-icon.png

# I would like to redirect all CSS, JS & Image files to:
https://s3-eu-west-1.amazonaws.com/mydomain.co.uk/static/$1

# So the URLs should be
https://s3-eu-west-1.amazonaws.com/mydomain.co.uk/static/styles/donate/style.css
https://s3-eu-west-1.amazonaws.com/mydomain.co.uk/static/js/donate/jquery.min.js
https://s3-eu-west-1.amazonaws.com/mydomain.co.uk/static/images/donate/help-icon.png

# All other .php files, I want served using my normal secure.mydomain.co.uk
https://secure.mydomain.co.uk/other/php/files.php

I have tried the following, but it don't work as expected:

RewriteCond %{REQUEST_URI} ^secure.mydomain.co.uk/(.*)$ [NC]
RewriteRule ^secure.mydomain.co.uk/(.*) https://s3-eu-west-1.amazonaws.com/mydomain.co.uk/static/$1 [R=302,L,NC]

I have created a CNAME to point to our S3 bucket, but I cannot use a CNAME for the secure request (amazon don't allow this), So instead I would just like to forward all requests for assets (css, images & js files) to Amazon S3 to get the content via https via amazon.

share|improve this question
add comment

2 Answers

What you need to do is to fix your RewriteCond - currently it will redirect all requests, regardless of URI. You want to restrict it to only allow the filenames you want. Try this:

RewriteCond %{REQUEST_URI} ^secure.mydomain.co.uk/(.*).[php|js|css|gif|jpg]$ [NC]

This should redirect all files ending with php, js, css, gif, or jpg - and only those files.

share|improve this answer
add comment
up vote 0 down vote accepted

Figured out what I needed. Basically, I have assets on my server, I have copied them over to S3, and would now like to redirect all my links to my S3 bucket (So I can serve them via https).

RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^secure\.mydomain\.org.uk$ [NC]
RewriteRule filename\.min\.js https://s3-eu-west-1.amazonaws.com/yourbucket/path/to/js/filename.min.js [L,R=301,NC]
RewriteRule another\.min\.js https://s3-eu-west-1.amazonaws.com/yourbucket/path/to/js/another.min.js [L,R=301,NC]

So basically, all requests for filename.min.js are redirected to my amazon bucket, and served from there.

There is probably a better way to do this (I'm not great with .htaccess rules), but adding each file I need served individually seems to work. (I only need these files redirected).

I would like all other requests to my secure. subdomain to be served normally via my app & asset-store (And not redirected to amazon).

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.