Take the 2-minute tour ×
Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. It's 100% free, no registration required.

When I watch my site trough live preview the previewframe is left blank, and in the console there appears the error: `

XMLHttpRequest cannot load http://domain.com/template . No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://www.domain.com' is therefore not allowed access.

What is that?

share|improve this question

2 Answers 2

up vote 4 down vote accepted

Instead of allowing cross site access, I'd recommend that you instead just make sure that the site is only accessible through one domain (not both www.domain.com and domain.com). This will also improve your SEO ranking, since search engines will index both these domains as separate, duplicated content. You can rewrite the domain in your .htaccess, like this:

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

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

Notice that only one of these rules should be used, if you enable both you'll get a redirection loop.

Also, make sure you use the correct domain in your Craft config.

share|improve this answer
    
I generally agree with you, but does this make sense for multi-lang configs like: domain.de or domain.com ? –  Victor In Jun 24 at 13:20
1  
No, but there's nothing in the original question that indicates that the site is actually running two different domains for two different locales. It says that domain.com/template can't load because www.domain.com is the origin. I'd be surprised if the site in question actually needs to run on both www.domain.com and domain.com. –  André Elvan Jun 24 at 13:28
    
That's the even better solution. I use domains with a locale segment like so: domain.com/locale. But the error wasn't the locale. I was logged in under www.domain.com/locale so the url's didn't match because I defined domain.com/locale in the gneral configuration file. –  Kaspar Jun 24 at 13:32
1  
@Kaspar: Great you got it sorted. -- aelva: Toché ;) –  Victor In Jun 24 at 13:40

I assume you are running Craft on a multi-environment config with to different domains.

You are getting this error because modern browsers block cross-site requests, and the live-preview is probably trying to load the template from another domain.

You can tell your server to allow a cross-site requests by sending a CORS-header. This header is called Access-Control-Allow-Origin.

.

Try adding this to your .htaccess file:

Header add Access-Control-Allow-Origin "http://example.com"

This will allow all requests coming from example.com. To be safe add the header for each domain you use:

Header add Access-Control-Allow-Origin "http://example1.com"
Header add Access-Control-Allow-Origin "http://example2.com"
Header add Access-Control-Allow-Origin "http://example3.com"

Note: You need mod_headers activated. And if you know what you're doing, you could also set this up in: httpd.conf <VirtualHost>

share|improve this answer
    
It looks like the request is trying to login to the CP on the other domain, to get the preview. If you are using multiple-domains for one site, take a look at aelvan's answer. If you have multiple languages on different domains, I don't know how to fix the 404-error, sorry. Try asking the support. –  Victor In Jun 24 at 13:26
1  
Hi. Aelvan's solution worked. But thanks anyway :) –  Kaspar Jun 24 at 13:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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