Skip to main content
PHP Blog

Back to all posts

How to Enable Cors In Apache2 And Xampp?

Published on
4 min read
How to Enable Cors In Apache2 And Xampp? image

Best Tools for Configuring Servers to Buy in September 2026

1 Ansible for DevOps: Server and configuration management for humans

Ansible for DevOps: Server and configuration management for humans

BUY & SAVE
$9.99 $19.99
Save 50%
Ansible for DevOps: Server and configuration management for humans
2 Ubuntu Server 24.04 LTS for Beginners: A Complete Guide to Installing, Configuring, and Managing Ubuntu Servers with Real-World Examples

Ubuntu Server 24.04 LTS for Beginners: A Complete Guide to Installing, Configuring, and Managing Ubuntu Servers with Real-World Examples

BUY & SAVE
$20.00
Ubuntu Server 24.04 LTS for Beginners: A Complete Guide to Installing, Configuring, and Managing Ubuntu Servers with Real-World Examples
3 Quick Rack - Essential for Network pros! Simplify Router, Switch, and Server Setup. Compatible with 19" Rails, Square, and Threaded Holes, ensuring a for Diverse Rack configurations

Quick Rack - Essential for Network pros! Simplify Router, Switch, and Server Setup. Compatible with 19" Rails, Square, and Threaded Holes, ensuring a for Diverse Rack configurations

  • HASSLE-FREE SETUP IN SECONDS WITH PATENTED QUICK RACK TOOL!
  • SUPPORTS UP TO 125LBS FOR SECURE, RELIABLE EQUIPMENT INSTALLATION.
  • SOLO EFFICIENCY: INSTALL HEAVY GEAR WITHOUT EXTRA HELP NEEDED!
BUY & SAVE
$249.00
Quick Rack - Essential for Network pros! Simplify Router, Switch, and Server Setup. Compatible with 19" Rails, Square, and Threaded Holes, ensuring a for Diverse Rack configurations
4 IT-Guy.IO ServerConnect Pro Portable Server Management Tool: USB Crash Cart Adapter – 1920 x 1200 – Portable Laptop USB 2.0 to KVM Console - Datacenter Server Monitor Mouse and Keyboard to USB

IT-Guy.IO ServerConnect Pro Portable Server Management Tool: USB Crash Cart Adapter – 1920 x 1200 – Portable Laptop USB 2.0 to KVM Console - Datacenter Server Monitor Mouse and Keyboard to USB

  • TRANSFORM ANY LAPTOP INTO A MOBILE SERVER MANAGEMENT TOOL.
  • EASY CONNECTION WITHOUT DRIVER INSTALLATION FOR QUICK ACCESS.
  • COMPACT DESIGN WITH POWER-EFFICIENT CAPABILITIES FOR TRAVEL.
BUY & SAVE
$199.00
IT-Guy.IO ServerConnect Pro Portable Server Management Tool: USB Crash Cart Adapter – 1920 x 1200 – Portable Laptop USB 2.0 to KVM Console - Datacenter Server Monitor Mouse and Keyboard to USB
5 Sipeed NanoKVM Black Full Mini Remote Control Operation and Maintenance Server Beyond RaspberryPi

Sipeed NanoKVM Black Full Mini Remote Control Operation and Maintenance Server Beyond RaspberryPi

  • TWO COLOR OPTIONS: CHOOSE BLACK OR WHITE TO MATCH YOUR STYLE!

  • MULTI-FUNCTION INTERFACE: CONNECT HDMI & USB FOR VERSATILE CONTROL.

  • REMOTE SERVER MANAGEMENT: MONITOR & CONTROL SERVERS IN REAL TIME!

BUY & SAVE
$58.00
Sipeed NanoKVM Black Full Mini Remote Control Operation and Maintenance Server Beyond RaspberryPi
6 Linux Server Hacks, Volume Two: Tips & Tools for Connecting, Monitoring, and Troubleshooting

Linux Server Hacks, Volume Two: Tips & Tools for Connecting, Monitoring, and Troubleshooting

BUY & SAVE
$32.07 $39.99
Save 20%
Linux Server Hacks, Volume Two: Tips & Tools for Connecting, Monitoring, and Troubleshooting
7 Ship an MCP Server in Python - Fast: Build, test, and deploy a production-ready MCP server with MCP Inspector, mcp.json, and Streamable HTTP

Ship an MCP Server in Python - Fast: Build, test, and deploy a production-ready MCP server with MCP Inspector, mcp.json, and Streamable HTTP

BUY & SAVE
$6.99
Ship an MCP Server in Python - Fast: Build, test, and deploy a production-ready MCP server with MCP Inspector, mcp.json, and Streamable HTTP
+
ONE MORE?

To enable CORS in Apache2 and XAMPP, you will need to configure the Apache web server by adding a few lines of code to the .htaccess file or the Apache configuration file (httpd.conf).

In the configuration file, you will need to include 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, Authorization"

These lines allow cross-origin requests from any domain, specify the allowed request methods, and define the allowed headers. Make sure to restart the Apache server after making these changes for the configuration to take effect.

For XAMPP, the process is similar. Locate the httpd.conf file in the XAMPP installation directory and add the above lines to enable CORS. Save the changes and restart Apache in XAMPP to apply the configuration.

Enabling CORS in Apache2 and XAMPP allows your web server to respond to cross-origin requests and allow resources to be shared between different domains. This is important for integrating web applications that make requests to APIs or resources from different origins.

What is the purpose of enabling CORS in both Apache2 and XAMPP?

Enabling CORS (Cross-Origin Resource Sharing) in both Apache2 and XAMPP allows for web applications running on different domains to make requests to resources on the server. This is important for web applications that require resources from different origins to function properly, such as APIs or content from external sources.

By enabling CORS in these web servers, developers can specify which origins are allowed to access resources on the server, helping to prevent unauthorized access and protect sensitive data. This can improve the security and functionality of web applications that rely on cross-origin requests.

How to enable preflight requests in Apache2 and XAMPP?

To enable preflight requests in Apache2 and XAMPP, you need to configure CORS (Cross-Origin Resource Sharing) headers in the apache configuration file. Here's how you can do it:

  1. Edit the Apache configuration file (httpd.conf) or the virtual host file to add the following lines:

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

  1. Make sure the "mod_headers" module is enabled in Apache. You can enable it by running the following command:

sudo a2enmod headers

  1. Restart the Apache server to apply the changes:

sudo systemctl restart apache2

After following these steps, preflight requests should be enabled in Apache2 and XAMPP. This will allow your web application to send cross-origin requests without any issues.

How to automatically apply CORS settings in Apache2 and XAMPP?

To automatically apply CORS settings in Apache2 and XAMPP, you can use a configuration file that specifies the CORS headers to include in HTTP responses. Here's how you can do it:

  1. Open the Apache configuration file for your virtual host or global configuration. This file is usually located in the conf directory of your Apache installation.
  2. Add the following lines to enable the mod_headers module, which is required to set CORS headers:

LoadModule headers_module modules/mod_headers.so

  1. Add the following lines to set the CORS headers in your Apache configuration file. Replace example.com with the domain you want to allow cross-origin requests from:

<IfModule mod_headers.c> Header set Access-Control-Allow-Origin "http://example.com" Header set Access-Control-Allow-Methods "GET, POST, OPTIONS" Header set Access-Control-Allow-Headers "Content-Type"

  1. Save the changes to your Apache configuration file and restart Apache to apply the changes.

If you are using XAMPP, you can follow the same steps above to add the CORS settings to the Apache configuration file included in your XAMPP installation.

By configuring CORS headers in your Apache configuration file, you can ensure that CORS settings are automatically applied to all requests handled by your Apache server. This can help prevent cross-origin request issues and improve security for your web applications.