• Home
  • Cloud VPS
    • Hong Kong VPS
    • US VPS
  • Dedicated Servers
    • Hong Kong Servers
    • US Servers
    • Singapore Servers
    • Japan Servers
  • Company
    • Contact Us
    • Blog
logo logo
  • Home
  • Cloud VPS
    • Hong Kong VPS
    • US VPS
  • Dedicated Servers
    • Hong Kong Servers
    • US Servers
    • Singapore Servers
    • Japan Servers
  • Company
    • Contact Us
    • Blog
ENEN
  • 简体简体
  • 繁體繁體
Client Area

Apache for Newbie: Set up Apache with mod_headers

December 17, 2023

Apache for Newbie: Set up Apache with mod_headers

When it comes to web servers, Apache is one of the most popular choices among website owners and developers. It is a powerful and flexible server that can be easily configured to meet the specific needs of your website. In this article, we will explore how to set up Apache with mod_headers, a module that allows you to modify HTTP request and response headers. This can be useful for a variety of purposes, such as improving security, optimizing caching, or enhancing user experience.

What is mod_headers?

Mod_headers is an Apache module that provides a simple and flexible way to manipulate HTTP headers. With mod_headers, you can add, modify, or remove headers in both incoming requests and outgoing responses. This can be done based on various conditions, such as the requested URL, the user agent, or the response status code.

Enabling mod_headers

Before we can start using mod_headers, we need to make sure it is enabled in our Apache server. To do this, we can use the following command:

sudo a2enmod headers

This command enables the mod_headers module in Apache. Once enabled, we need to restart the Apache service for the changes to take effect:

sudo service apache2 restart

Adding Custom Headers

Now that mod_headers is enabled, we can start adding custom headers to our Apache configuration. To do this, we need to edit the Apache configuration file, which is usually located at /etc/apache2/apache2.conf or /etc/httpd/conf/httpd.conf.

Open the configuration file using a text editor and add the following lines:

Header set X-My-Custom-Header "Hello, Server.HK!"

This line adds a custom header named “X-My-Custom-Header” with the value “Hello, Server.HK!” to all outgoing responses. You can replace the value with any text or variable you want.

Modifying Existing Headers

In addition to adding custom headers, mod_headers also allows us to modify existing headers. For example, let’s say we want to modify the “Cache-Control” header to enable caching for certain file types. We can use the following configuration:

<FilesMatch ".(jpg|jpeg|png|gif|ico|css|js)$">
    Header set Cache-Control "public"
</FilesMatch>

This configuration sets the “Cache-Control” header to “public” for all files with the specified extensions. This can help improve the caching behavior of your website and enhance its performance.

Removing Headers

Sometimes, you may want to remove certain headers from the HTTP response. This can be done using mod_headers as well. For example, let’s say we want to remove the “Server” header, which reveals information about the server software being used. We can use the following configuration:

Header unset Server

This configuration removes the “Server” header from all outgoing responses, enhancing the security of your website by hiding server details from potential attackers.

Summary

Apache with mod_headers provides a powerful tool for manipulating HTTP headers in both incoming requests and outgoing responses. By adding custom headers, modifying existing headers, or removing unnecessary headers, you can enhance the security, performance, and user experience of your website. Remember to enable mod_headers in your Apache server and make the necessary configurations in the Apache configuration file. With these techniques, you can take full control of your website’s headers and optimize its behavior according to your specific requirements.

For reliable and high-performance VPS hosting solutions, consider Server.HK. With Hong Kong VPS Hosting, you can enjoy the benefits of a virtual private server tailored to your needs. Visit https://server.hk to learn more about our hosting services and find the perfect hosting plan for your website.

Recent Posts

  • How to Migrate from CentOS 8 to AlmaLinux or Rocky Linux Safely
  • CentOS Server Performance Tuning: Optimization Techniques for 2026
  • How to Configure SELinux in CentOS Without Breaking Your System (CentOS Stream 9/10 – 2026)
  • Managing Users and Permissions in CentOS Stream: Best Practices (CentOS Stream 9/10 – 2026)
  • How to Set Up Nginx on CentOS Stream for High-Performance Web Hosting

Recent Comments

No comments to show.

Knowledge Base

Access detailed guides, tutorials, and resources.

Live Chat

Get instant help 24/7 from our support team.

Send Ticket

Our team typically responds within 10 minutes.

logo
Alipay Cc-paypal Cc-stripe Cc-visa Cc-mastercard Bitcoin
Cloud VPS
  • Hong Kong VPS
  • US VPS
Dedicated Servers
  • Hong Kong Servers
  • US Servers
  • Singapore Servers
  • Japan Servers
More
  • Contact Us
  • Blog
  • Legal
© 2026 Server.HK | Hosting Limited, Hong Kong | Company Registration No. 77008912
Telegram
Telegram @ServerHKBot