enable cors in .htaccess

To enable Cross-Origin Resource Sharing (CORS) in an .htaccess file, you can add the following lines:

Header set Access-Control-Allow-Origin "*"
Header set Access-Control-Allow-Methods "GET, POST, PUT, DELETE, OPTIONS"
Header set Access-Control-Allow-Headers "Content-Type"

This will allow any origin to access resources on your server, using any method and any Content-Type.

Watch a course Learn object oriented PHP

You can also specify a particular origin by replacing the "*" with the URL of the origin. For example:

Header set Access-Control-Allow-Origin "https://example.com"

This will allow https://example.com to access resources on your server, but block all other origins.

You can also specify multiple origins by separating them with a comma:

Header set Access-Control-Allow-Origin "https://example.com, https://othersite.com"

This will allow https://example.com and https://othersite.com to access resources on your server, but block all other origins.

Note that the .htaccess file must be located in the same directory as the resources that you want to allow CORS for, or in a parent directory.