1

I'm attempting to get around some Cross Origin issues on IE11 and iOS by masking a subdomain as a directory

i.e.

I want example.com/cdn/ to actually load cdn.example.com/

I've kind of got it working with

RedirectMatch 301 ^/cdn/(.*)$ http://cdn.example.com/$1

But in the browser the redirect happens and it still shows http://cdn.example.com rather than leaving it as example.com/cdn/

Any way to achieve this!?

  • 1
    A redirect is the wrong approach. Either you proxy the requests to the subdomain or you do it the right way and allow the subdomain for cross origin requests by adding the proper headers. – Gerald Schneider Sep 13 '16 at 8:16
  • Hmm thanks could you enlighten me on either of those options? I think I have added the proper headers but it's still getting blocked in IE <Edge and iOS. This is another approach I thought I'd try. – beek Sep 13 '16 at 8:19
2

You can use mod_proxy to proxy the requests from the directory through to the actual domain:

ProxyRequests         Off
ProxyPreserveHost     On

ProxyPass             /cdn           http://cdn.example.com
ProxyPassReverse      /cdn           http://cdn.example.com

This has to be put in the actual apache config, it doesn't work in a .htaccess file.

Note that by proxying the requests like this you are rendering the advantages of a CDN useless. The header solution in my other answer is preferable over this.

  • This is working.. but it means not using the CDN on the scenarios where I'm having problems (IE11 and iOS) – beek Sep 13 '16 at 9:04
2

You can add the Access-Control-Allow-Origin header to the responses your server provides.

You can to this either in your scripts (PHP or whatever you run on the server) or you can do this in the .htaccess file using mod_headers:

<IfModule mod_headers.c>
    Header set Access-Control-Allow-Origin "http://cdn.example.com"
</IfModule>
  • ...without having to mask the subdomain. – MrWhite Sep 13 '16 at 8:32
  • This is without masking. You are masking it if you use the proxy solution. – Gerald Schneider Sep 13 '16 at 8:33
  • Hmm yeah that makes sense - it's just not making any difference at the moment, I still get loads of security errors when I try and load video from the cdn into a WebGL renderer on IE11 or iOS browsers – beek Sep 13 '16 at 9:01
  • Question - if this is coming via the Java Server (Spring) would the .htaccess still work or do I need to use something like this via Spring? – beek Sep 13 '16 at 9:29
  • Both should work. – Gerald Schneider Sep 13 '16 at 9:33

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.