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)

# I would like to redirect all CSS, JS & Image files to:

# So the URLs should be

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

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.


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.


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).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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