2

I have a site site.domain1.com that needs to launch content from another domain - content.domain2.com but I encounter cross-domain JS issues when doing this directly.

I basically need to make site.domain1.com think that the content from domain2 is actually from domain1.com. I have read that URL rewrite in IIS7 may be the answer, can this be done?

3

The problem you're having is solved with CORS:

The Cross-Origin Resource Sharing standard works by adding new HTTP headers that allow servers to describe the set of origins that are permitted to read that information using a web browser. Additionally, for HTTP request methods that can cause side-effects on user data (in particular, for HTTP methods other than GET, or for POST usage with certain MIME types), the specification mandates that browsers "preflight" the request, soliciting supported methods from the server with an HTTP OPTIONS request method, and then, upon "approval" from the server, sending the actual request with the actual HTTP request method. Servers can also notify clients whether "credentials" (including Cookies and HTTP Authentication data) should be sent with requests.

https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS

You need to add headers, from the server, to the response telling the browser that made the request that you'll trust other origin domains. That link reviews the process and the required headers. You add headers such as:

Access-Control-Allow-Origin "*";
Access-Control-Allow-Methods "GET, POST, PUT, OPTIONS, DELETE";
Access-Control-Allow-Headers "Authorization,Content-Type,Accept";
Access-Control-Allow-Credentials "true";

The first header tells the browser to allow cross server requests from your site's JS code. There are security concerns with this approach, which is why this is not allowed directly in the first place.

0

Rewriting URLs alone will not solve your problem. Rewrite can only rewrite local URLs, not URLs on other sites.

You may be looking for the concept of a "reverse proxy". That is, having your webserver grab content from another site and republish it dynamically on your own domain. Then your JavaScript could access this data without cross site problems.

Here is an article about setting up reverse proxy using IIS, URL Rewrite, and ARR.

You could also look into cross domain AJAX. It looks like JSONP or Access-Control-Allow-Origin could be good options for you.

  • Hi, thanks for your reply! In this case, I would host domain2 and a client domain1 and I need my files to act like they have been pulled from the client's domain. From reading about the reverse proxy it sounds as though this is something the client would have to do? Are there any alternatives? – U01SFA3 May 13 '14 at 13:10
  • If you want something to appear to come from the client's site, the client is going to have to do something. I added some links for cross domain AJAX to the answer that may help you. – Stephen Ostermiller May 13 '14 at 13:14

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.