Modify .hcacess on Apache Server to allow CORS for specified domains

Motivation

I purchased a .tech domain recently, and made it a alias domain to hollen9.com.
However, some src request are blocked due to CORS policy.
Typical symptoms are blank images, failure to load external fonts, scripts, etc. You can verify it by checking your dev console on the browser by pressing Shift+Ctrl / Command+i.

Solution

Locate your .htacess file of your website, for analog it is a config file just like web.config on IIS.
Open it with a text editor, insert the following:

<IfModule mod_headers.c>
    # Allow loading of alias domain resources
    # You should modify the domains to suit your need.
    SetEnvIf Origin "http(s)?://(www\.)?(holllen.tech|hollen9.com)$" AccessControlAllowOrigin=$0
    Header add Access-Control-Allow-Origin %{AccessControlAllowOrigin}e env=AccessControlAllowOrigin
    Header merge Vary Origin
</IfModule>

Force reload your previous page by pressing Ctrl+F5 or Command+Shift+R, and viola! Now all images are visible!

References

張貼在 網站開發, 軟體工程 並標記 ,

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。