Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»Web Servers»Apache»How to Enable CORS in Apache Web Server

    How to Enable CORS in Apache Web Server

    RahulBy RahulFebruary 13, 20171 Min ReadUpdated:January 19, 2022

    Cross-Origin Resource Sharing (CORS) is the process, which tells the web browsers to allows resources running form different origins (domain, protocol, or port) via HTTP headers. This tutorial will help you to enable CORS in the Apache webserver.

    Prerequisites

    You must have enabled Apache headers modules. For the Ubuntu and other Debian based systems execute the following command to enable headers modules.

    sudo a2enmod headers 

    In CentOS and other Redhat based Linux systems, edit the Apache configuration file httpd.conf and uncomment the following line by removing "#" in front of them.

    LoadModule headers_module modules/mod_headers.so
    

    Enable CORS in Apache

    Set Access-Control-Allow-Origin (CORS) authorization to the header in Apache web server. Add the following line inside either the <Directory>, <Location>, <Files> sections under <VirtualHost> in Apache configuration files. You can also place this inside the .htaccess file.

     Header set Access-Control-Allow-Origin "*"
    

    Example

    To allow Access-Control-Allow-Origin (CORS) authorization for all origin domains for all files inside a directory.

    Apache
    1
    2
    3
    4
    5
    <Directory "/path/to/dir">
       <IfModule mod_headers.c>
            Header set Access-Control-Allow-Origin "*"
       </IfModule>
    </Directory>

    To allow Access-Control-Allow-Origin (CORS) authorization for specific files only. For example to allow CORS for fonts only use following example:

    Apache
    1
    2
    3
    4
    5
    <FilesMatch "\.(ttf|otf|eot|woff)$">
        <IfModule mod_headers.c>
            Header Set Access-Control-Allow-Origin "*"
        </IfModule>
    </FilesMatch>

    To allow Access-Control-Allow-Origin (CORS) with multiple origin domains, Use following example

    Apache
    1
    2
    3
    4
    5
    6
    <FilesMatch "\.(ttf|otf|eot|woff)$">
        <IfModule mod_headers.c>
            SetEnvIf Origin "http(s)?://(www\.)?(google.com|staging.google.com|development.google.com)$" AccessControlAllowOrigin=$0
            Header add Access-Control-Allow-Origin %{AccessControlAllowOrigin}e env=AccessControlAllowOrigin
        </IfModule>
    </FilesMatch>

    After making changes in configuration files, You need to restart the Apache webserver. But no need to restart if adding in the .htaccess file.

    Restart Apache Web Server

    sudo systemctl restart apache2 
    

    Conclusion

    Apache CORS
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleHow to Reckon Your Real Internet Speed?
    Next Article How to Install Apache Maven on Windows

    Related Posts

    How to Install Apache ActiveMQ on Ubuntu 22.04

    3 Mins Read

    How To Install LAMP Stack on Ubuntu 22.04 LTS

    Updated:April 20, 20225 Mins Read

    How To Disable HTTP Methods in Apache

    Updated:December 31, 20212 Mins Read

    How To Setup Apache, PHP & MongoDB in Ubuntu & Debian

    Updated:October 8, 20213 Mins Read

    Common Apache Commands on Ubuntu & Debian

    4 Mins Read

    How to Install and Secure Apache on Debian11

    6 Mins Read

    1 Comment

    1. phil on May 7, 2020 7:11 pm

      Thanks for this – was having real issues serving an API to an angular SPI due to cross domain. Many solutions offer allow-origin “*” but this doesn’t work as angular sends credentials (can’t have allow-credentials with origin “*”)

      One issue – for me the $0 argument is always null. I made a work around with multiple lines and hard coding each assignment, but your version is more elegant.

      $0 looks like an parameter variable but I can’t find any information about using these in this context.

      Could you help point me to where can I find out information about this?

      Thanks

      Reply

    Leave A Reply Cancel Reply

    Recent Posts
    • How to Enable / disable Firewall in Windows
    • How to Install JAVA on Ubuntu 22.04
    • Switching Display Manager in Ubuntu – GDM, LightDM & SDDM
    • Changing the Login Screen Background in Ubuntu 22.04 & 20.04
    • How To Install PHP (8.1, 7.4 or 5.6) on Ubuntu 22.04
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.