How to Add Custom Response Header in Apache?
Last Updated :
14 Jun, 2024
Apache HTTP server is most widely used web server. it is known for its flexibility and extensive features. One of those great features is response headers. Custom response headers can be helpful for various purposes, such as enhancing security, improving caching mechanisms, or adding custom metadata to HTTP response.
What are HTTP headers?
HTTP headers are key-price pairs dispatched between the consumer (normally web browser) and the server. These headers comprise crucial records about the request or reaction, including content type, caching rules, and authentication details. HTTP headers are divided into categories:
- Request Headers: Sent via the purchaser to the server.
- Response Headers: Sent by means of the server to the consumer.
Adding Custom Headers in Apache on Windows
1. Enable the mod_headers Module:
1. Open Command Prompt as Administrator:
- Press Win + X and select Command Prompt (Admin) or Windows PowerShell (Admin).
2. Navigate to Apache's bin Directory:
cd C:\Apache24\bin
3. Check if mod_headers is Enabled:
httpd -M | findstr headers
- If you see headers_module (shared), the module is enabled.
- If not, proceed to enable it.
4. Enable mod_headers in Configuration:
- Open C:\Apache24\conf\httpd.conf in a text editor like Notepad.
- Find the line #LoadModule headers_module modules/mod_headers.so and remove the # to uncomment it.
LoadModule headers_module modules/mod_headers.so
5. Restart Apache:
httpd -k restart
2. Add Custom Headers in the Configuration File
1. Open httpd.conf:
- Open C:\Apache24\conf\httpd.conf in a text editor.
2. Add Custom Headers:
- Add the following lines at the end of the file to add a custom header:
<IfModule mod_headers.c>
# Add a custom header for all responses
Header add X-Custom-Header "geek for geeks"
# Add a custom security header
Header add Strict-Transport-Security "max-age=31536000; includeSubDomains; preload"
# Add CORS headers
Header add Access-Control-Allow-Origin "*"
Header add Access-Control-Allow-Methods "GET, POST, OPTIONS"
Header add Access-Control-Allow-Headers "Content-Type, Authorization"
</IfModule>
- Replace "X-Custom-Header" and "geek for geeks" with your desired header name and value.
3. Save and Close the File.
4. Restart Apache:
httpd -k restart
3. Verifying Custom Headers
- Open Developer Tools: Press F12 or right-click on the page and select Inspect.
- Go to the Network Tab: Click on the Network tab in the Developer Tools.
- Refresh the Page: Press F5 or click the refresh button in your browser to reload the page.
- Select the Request: Find and click on the request that corresponds to your website URL in the list of network requests.
- Check the Headers: In the right pane, click on the Headers tab. Scroll down to find the Response Headers section. Locate your custom header (X-Custom-Header).
output verify in browser network tab.
Similar Reads
How to Install and Customize Apache on Ubuntu or Debian? One of the most dominant forces in web server software is the Apache HTTP Server, often shortened to Apache. This free and open-source project, overseen by the Apache Software Foundation, has been a major player in shaping the Internet since its launch in 1995. Installing an Apache web server on Ubu
2 min read
How to Enable CORS in Apache Web Server? Cross-Origin Resource Sharing(CORS) is a security feature that allows web browsers to make requests to a different domain than the one serving the web page. without CORS, browsers restrict such requests due to security concerns. Enabling CORS ensures that your web server responds correctly to cross-
2 min read
How to add HTTP headers 'X-Frame-Options' on iframe ? Inline frame tag in HTML: The iframe tag is used to displaying or embedding another document within an HTML document. One of its attributes 'src' is used to specify the URL of the document which is to be displayed. A site's X-frame Options can prevent allowing the display of one HTML document within
2 min read
How to Set HTTP Headers Using Apache Server? HTTP headers are key-value pairs sent in HTTP requests and responses. They carry essential information about the request or response, such as content type, caching directives, and security policies. Setting HTTP Headers in Apache is a common requirement for enhancing the security, performance and fu
4 min read
How to send HTTP response code in PHP? HTTP Response code: There are three approaches to accomplish the above requirement, depending on the version. For PHP versions 4.0: In order to send the HTTP response code, we need to assemble the response code. To achieve this, use header() function. The header() function contains a special use-cas
2 min read
How to set header request in Postman? Postman is a powerful API development tool that offers a feature known as environment variables. These variables are used for efficient testing and development of APIs by allowing users to manage dynamic values across requests easily. In this article, we will learn How you can set header requests in
2 min read