我的同一个域上没有 'Access-Control-Allow-Origin',但是 w/o 'www'

No 'Access-Control-Allow-Origin' on my same domain but w/o 'www'

不确定我在 htaccess 中做错了什么,但是当我从 http://example.org 而不是 http://www.example.org 访问我的网站时,它会抛出此错误:

 No 'Access-Control-Allow-Origin' header is present on the requested resource.
 Origin 'http://example.org' is therefore not allowed access.

如何设置我的 htaccess 或我的服务器 apache 以避免这个问题?

www.example.comexample.com是不同的来源。要成为同源,完整 主机名、端口和方案必须匹配。

您有三个选择:

  • 设置 CORS 以将访问权限从一个授予另一个。
  • 执行 Ajax 时,使用相对 URI 而不是绝对 URI。
  • 选择一个成为规范的,并停止在另一个之外托管您的网站。

最后一种方法可能是最好的。

  1. 将所有链接更改为相对链接或指向 example.com
  2. 为 www.example.com 上的所有内容设置 HTTP 301 重定向到 example.com