How to enable CORS for your website?

A request for the website resource like images or fonts from outside of the origin website is known as a cross-origin request. It is a security mechanism that allows restricted resources like fonts, JavaScript code etc. on a website page to be requested from a 3rd party domain outside the domain from which these resources were served. Using CORS, you can control the website resources on who can access it.

CORS on a website hosted on Linux Environment

You can add the below line in your .htaccess file where cross-domain scripting is required.

<ifModule mod_headers.c>
Header set Access-Control-Allow-Origin: *
</ifModule>

CORS on a website hosted on Windows Environment

For Windows hosting accounts, create or modify the web.config file in the directory where you want to permit CORS requests. Add the following lines to the web.config file:

To enable CORS on the windows website, you can just add the below lines.

<configuration>
   <system.webServer>
     <httpProtocol>
       <customHeaders>
           <add name="Access-Control-Allow-Origin" value="*" />
        </customHeaders>
     </httpProtocol>
   </system.webServer>
</configuration>

Related Articles

Receiving unexpected T_STRING error.

If you are receiving this error, this means .html files are parsed as PHP. In case, your .html...

How to Serve Scaled Images on Your WordPress Website using plugins?

If you have tested your website in a performance testing tool and asking you to serve the scaled...

How to modify a hosts file in Windows 7, Windows 8/10, Mac OS and Ubuntu?

By Editing Host File, you can point IP Address for a Domain in your local system.   Windows 7 :...

NAT (Network Address Translation)

What is NAT? NAT (Network Address Translation) was introduced in the IT and Networking...

How to make your ASP.Net applications secure?

Adding security to Web apps may not be great fun but it doesn't have to be hard. You can easily...

  • 0 Users Found This Useful

Was this answer helpful?