I have 10 sub domains and getting all static files from a sub domain (static.88n8.com).

I want to allow cross origin for all sub domains in .htaccess with this:

SetEnvIf Origin "^http(s):\/\/(www\.google.com|google\.com|88n8\.com|second\.88n8\.com|third\.88n8\.com|fourth\.88n8\.com)$" OriginDomainStr=$0
Header add Access-Control-Allow-Origin %{OriginDomainStr}e env=OriginDomainStr

Or this one:

SetEnvIf Origin "^http(s):\/\/(www\.google.com|google\.com|88n8\.com|second\.88n8\.com|third\.88n8\.com|fourth\.88n8\.com)$" OriginDomainStr=$0
Header set Access-Control-Allow-Origin %{OriginDomainStr}e env=OriginDomainStr

Or this one:

SetEnvIf Origin "^http(s):\/\/(www\.google.com|google\.com|88n8\.com|second\.88n8\.com|third\.88n8\.com|fourth\.88n8\.com)$" OriginDomainStr=$0
Header always set Access-Control-Allow-Origin %{OriginDomainStr}e env=OriginDomainStr

And console showing me:

Access to font at 'https://static.88n8.com/font.woff' from origin 'https://88n8.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Which is very weird. That regex should match but seems it doesn't match at all.

I tried this one to check if I can set the header and it worked for main domain:

<IfModule mod_headers.c>
    Header set Access-Control-Allow-Origin "https://88n8.com"
</IfModule>

I don't want to use * or single domain. Why my regex never match in .htaccess? Seems my Regex test is OK.

Disclaimer: 88n8.com is an example to show my domain is including numbers and English characters in it. It's not my real domain and I don't own that.

Edit 1

As you can see in my REGEX, it should be applied to all 88n8.com sub domains and google.com (with https or http). I have only included 4 sub domains in my REGEX to show you my REGEX.

Edit 2

I've added this code to my PHP pages to know current Origin:

echo (isset($_SERVER['HTTP_ORIGIN']))? $_SERVER['HTTP_ORIGIN'] : 'NOT SET';

and the result is NOT SET. Seems my Origin is empty or NULL and That's the reason REGEX never match.

1 Answers

1
WoLfulus On

You're accessing from static subdomain. It's not on the regex of approved subdomains.

I'd also suggest combining the subdomains.

^http(s):\/\/((www\.)?google\.com|(first|second|third|fourth|static)\.88n8\.com)$

All subdomains from 88n8.com and all subdomains from google.com

^http(s):\/\/((www\.)?google\.com|(.+\.)?88n8\.com)$