How to Allow Cross domain request in Apache2
To allow Cross-Origin Resource Sharing (CORS) requests on an Apache2 web server, you can add the necessary headers to the server's response using a .htaccess file or by modifying the server's configuration file.
Apache Configuration File :
/etc/apache2/httpd.conf
/etc/apache2/apache2.conf
/etc/httpd/httpd.conf
/etc/httpd/conf/httpd.conf
Here are the steps to add CORS headers using a .htaccess file:
1. Create a .htaccess file in the root directory of your web application if one doesn't already exist.
2. Add the following lines to the .htaccess file to enable CORS:
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, Authorization"
The `Access-Control-Allow-Origin` header allows requests from any domain, which can be changed to allow requests from specific domains by replacing the `*` with the appropriate domain.
The `Access-Control-Allow-Methods` header specifies the HTTP methods that are allowed for cross-origin requests.
The `Access-Control-Allow-Headers` header specifies the HTTP headers that are allowed for cross-origin requests.
3. Save the .htaccess file and upload it to the root directory of your web application.
4. Test the CORS headers by sending a cross-origin request to your web application and checking the response headers.
Note that enabling CORS can introduce Security Vulnerabilities, so it's important to only allow cross-origin requests from trusted sources and to use appropriate authentication and authorization mechanisms.
Other Solution
Edit nano /etc/apache2/apache2.conf
<Directory /var/www/html>
Order Allow,Deny
Allow from all
AllowOverride all
Header set Access-Control-Allow-Origin "*"
</Directory>
Add/activate module (Ubuntu/Debian)
sudo a2enmod headers
Add/activate module (Ubuntu/Debian) CentOS/Redhat/Fedora
In CentOS/RHEL/Fedora, open the Apache/http configuration file httpd.conf and uncomment the following line by removing # in front of them.
LoadModule headers_module modules/mod_headers.so
First, headers_module corresponds to mod_headers:
grep headers_module /etc/httpd/conf.modules.d/*
/etc/httpd/conf.modules.d/00-base.conf:LoadModule headers_module modules/mod_headers.so
Then the command; it is described in httpd -h as:
-t -D DUMP_MODULES : show all loaded modules
Restart service by below Command
systemctl restart apache2 restart Or /etc/init.d/apache2 restart