Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I am in the progress of migrating an old banner system to a new one and in doing so I want to rewrite the old banner system's URL's to the new one. I load my banners via an AJAX request, and therefore I require the Access-Control-Allow-Origin to be set to *.

I have the following VirtualHost configuration:

<VirtualHost *:80>
    ServerAdmin webmaster@all4students.nl
    ServerName banner.studenten.net

    # we want to allow XMLHTTPRequests
    Header set Access-Control-Allow-Origin "*"

    RewriteEngine on
    RewriteMap bannersOldToNew txt:/home/user/banner.studenten.net/banner-studenten-net-to-ads-all4students-nl-map

    # check whether a zoneid exists in the query string
    RewriteCond %{QUERY_STRING} ^(.*)zoneid=([1-9][0-9]*)(.*)
    # make sure the requested banner has been mapped
    RewriteCond ${bannersOldToNew:%2|NOTFOUND} !=NOTFOUND
    # rewrite to ads.all4students.nl
    RewriteRule ^/ads/.* http://ads.all4students.nl/delivery/ajs.php?%1zoneid=${bannersOldToNew:%2}%3 [R]
    # else 404 or something

    ErrorLog ${APACHE_LOG_DIR}/banner.studenten.net-error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

    CustomLog ${APACHE_LOG_DIR}/banner.studenten.net-access.log combined

</VirtualHost>

My map file, /home/user/banner.studenten.net/banner-studenten-net-to-ads-all4students-nl-map, contains something like:

# oldId newId
140 11
141 12
142 13

Based on the above configuration I was expecting the following:

GET /ads/ajs.php?zoneid=140 HTTP/1.1
Host: banner.studenten.net

HTTP/1.1 302 Found
...
Access-Control-Allow-Origin: *
Location: http://ads.all4students.nl/delivery/ajs.php?zoneid=11

But instead I get the following:

GET /ads/ajs.php?zoneid=140 HTTP/1.1
Host: banner.studenten.net

HTTP/1.1 302 Found
...
Location: http://ads.all4students.nl/delivery/ajs.php?zoneid=11

Note the missing Access-Control-Allow-Origin header, this means the XMLHttpRequest is denied and the banner is not displayed. Any suggestions on how to fix this in Apache?

share|improve this question

migrated from stackoverflow.com Apr 11 '12 at 16:43

This question came from our site for professional and enthusiast programmers.

1 Answer 1

up vote 0 down vote accepted

I found a solution to this problem, apparently Apache Header set only applies to 2xx status codes (assumption based on http://httpd.apache.org/docs/2.0/mod/mod_headers.html#header). To get the access control header to work in my configuration I had to type Header always set:

<VirtualHost *:80>
    ...

    # we want to allow XMLHTTPRequests
    Header always set Access-Control-Allow-Origin "*"

    ...

</VirtualHost>
share|improve this answer

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.