Mobile app version of vmapp.org
Login or Join
Smith883

: Can apache both redirect and set CORS headers? I encountered the following browser console message, using apache to redirect from http to https XMLHttpRequest cannot load http://xxx. Redirect

@Smith883

Posted in: #Apache

I encountered the following browser console message, using apache to redirect from http to https


XMLHttpRequest cannot load xxx. Redirect from 'http://xxx' to 'https://xxx' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://current_page' is therefore not allowed access.


Even though xxx has correct CORS headers set.

Turns out that apache's "redirect all" directive:

<VirtualHost *:80>
ServerName servername
Redirect permanent / servername/ </VirtualHost>


apparently needs to include a CORS header or the redirect itself will be rejected by web browser.

Is it possible to have apache's "redirect permanent" also include CORS headers?

I tried this, from random googling:

<VirtualHost *:80>
ServerName xxx
# hope this preserves url's for the auto redir :)
Header always set Access-Control-Allow-Origin "*"
Header always set Access-Control-Allow-Methods "POST, GET, OPTIONS, DELETE, PUT"
Header always set Access-Control-Max-Age "1000"
Header always set Access-Control-Allow-Headers "x-requested-with, Content-Type, origin, authorization, accept, client-security-token"
Redirect permanent / xxx/ </VirtualHost>


Or any other work around here?

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Smith883

1 Comments

Sorted by latest first Latest Oldest Best

 

@Alves908

Setting the required HTTP response headers before the redirect (which sets the HTTP status and Location headers) should work.

However, you will need to make sure that all local caches are cleared before testing. The earlier permanent redirect (that didn't include the CORS headers) will have been cached, so any new requests will not hit your server and see the update until the cache is cleared.

The local cache would seem to have been the problem in this instance.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme