简体   繁体   English

Access-Control-Allow-Origin和www子域

[英]Access-Control-Allow-Origin and www subdomain

I am having troubles with Access-Control-Allow-Origin. 我在使用Access-Control-Allow-Origin时遇到麻烦。 I am using CodeIgniter (CI) & jQuery to make a jQuery.getJSON() call. 我正在使用CodeIgniter(CI)和jQuery进行jQuery.getJSON()调用。 I call jQuery.getJSON() using a CI URL. 我使用CI URL调用jQuery.getJSON()。 My base URL in the CI config includes the www (www.domain.com) in the domain name. 我在CI配置中的基本URL在域名中包含www(www.domain.com)。 When I am accessing the site, I am not using www (domain.com). 当我访问该网站时,我没有使用www(domain.com)。 So when the jQuery.getJSON() call is made it uses the www url, but it is being called from the non-www url. 因此,当进行jQuery.getJSON()调用时,它使用www url,但从非www url进行调用。 This is causing the error: 这导致错误:

Origin non-www.domain.com is not allowed by Access-Control-Allow-Origin. Access-Control-Allow-Origin不允许使用来源非www.domain.com。

How do you deal with this problem? 您如何处理这个问题? I have seen lots of posts about how to deal with different sub domains making these AJAX calls, but I feel like this is different. 我已经看到很多关于如何处理这些AJAX调用的不同子域的文章,但是我觉得这是不同的。 This is the same site, but some people may choose to use www.domain.com and some may choose to use just domain.com . 这是同一网站,但是有些人可能选择使用www.domain.com ,而有些人可能选择仅使用domain.com

I would suggest you pick once and for all whether or not you want www in front of your site and then setup apache redirect accordingly, which would save you a lot of headache: 我建议您一劳永逸地选择是否要在站点前使用www ,然后相应地设置apache重定向,这将为您节省很多麻烦:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain\.com
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=permanent,L] 

(this redirects non-www requests to www) (这会将非www请求重定向到www)

Thanks @serg. 谢谢@serg。 I ended up doing what you suggested, but just the inverse (redirected all www to non-www). 我最终做了您建议的事情,但只是相反(将所有www重定向为非www)。 I also found similar code to what you provided but it is more generic so I don't have to change the domain name when I want to reuse it: 我还发现了与您提供的代码类似的代码,但是它更通用,因此当我想重用它时不必更改域名:

RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM