cookies – apache mod expires


Setting up Cookies

A cookie is a small text file which is generally used to identify a visitor as first time/ returned visitor or member/non-member visitor. When a user request a page from your web server the server may place/read this text file on a user’s hard disk. You as an SEO must know how cookies are created and set up.

The setcookie() function is used to create a cookie. This function must appear BEFORE the <html> tag.

Syntax: setcookie(name, value, expire, path, domain,secure);

Name’ is the name of the cookie.
Value’ is the value associated with the cookie.
Expire’ is the time when the cookie should expire. For e.g. time()+3600*24*30 will set the cookie to expire in 30 days. If this parameter is not set then the cookie will expire at the end of the session (when the browser closes).

Path’ is where the cookie should be available. For e.g. within a particular directory (like /test/) or entire domain (like /)
Domain’ is the domain name of the cookie like
‘Secure’ is whether or not the cookie should be set over a secure HTTP connection. Its default value is ‘False’ which means cookie should be set up. The value of ‘True’ means cookie should be set up only if a secure connection exist.

Note: Only ‘name’ and ‘value’ fields are required in the setcookie() function. All other fields are optional.

Example 1

In the example below, we will create a cookie named “seomoz” and assign the value “Rand Fishkin” to it. We will also specify that the cookie should expire after one hour:

setcookie(“seomoz”, “Rand Fishkin”, time()+3600);


Example 2

In the example below, we will create a cookie named “seomoz” and assign the value “Rand Fishkin” to it. We will also specify that the cookie should expire after one month:

setcookie(“seomoz”, “Rand Fishkin”, time()+3600*24*30);


Note: In the example above the expiration time is set like this: 1 hr(3600 seconds) * 1 day(24hrs)*1 month(30 days). So what I have specified is the total number of seconds in a month.

Viewing Cookie Information

Once you have setup cookie, you can verify by it through ‘web developers’ toolbar. Install web developers toolbar ( Click on ‘view cookie information’ under the ‘cookies’ menu. You may see a long list of cookies. One of the cookie will be the one I just set up:

I set up this cookie on my own website That’s why the host name is Note this cookie will expire after one month. We can also set this cookie to expire after 6 months or 6 years.


Viewing HTTP response header

A browser and a web server communicate with each other through HTTP request and response headers. HTTP request headers are sent by a browser whenever it request a resource (like image file) from a web server. HTTP response headers are send by a web server in response to the request made by the web browser. You as an SEO must know what are HTTP headers, how they are setup and how they can be viewed. To view HTTP response header click on ‘view response headers’ under the ‘information’ menu on the web developers toolbar. Here is what i got:

PHP header() Function

This function is used to send a raw HTTP response header to a browser and must be called before an actual output is sent (in PHP 4 and above).

Syntax: header(string,replace,http_response_code);


String‘ is the header string to send. It is made up of colon separated name-value pairs. For. e.g. “Cache-Control: no-cache”. This field is required.

replace‘ specifies whether the header should replace previous header or add a second header. Default is TRUE (will replace). FALSE (allows multiple headers of the same type). This field is optional.

http_response_code‘ forces the HTTP response code to the specified value (only available in PHP 4.3 and higher).

Example 1:

Send status code 404

Place the following code on the page which should return a 404 response code

// Send 404 File not found response code
header(“HTTP/1.0 400”);



Example 2: Send status code 301

Place the following code on the page which should return a 301 response code

// Send 301 Moved permanently response code
header(“HTTP/1.0 301”);


Congratulations! you now know how to send HTTP status codes. Now you must be able to handle soft 404 issues. Click here to see the complete list of HTTP status codes.

Example 3: Prevent page caching:

Place the following code on the page which you don’t want to be cached.

// Date in the past
header(“Expires: Mon, 20 Jul 2001 12:00:00 GMT”);
header(“Cache-Control: no-cache”);
header(“Pragma: no-cache”);


Example-4: Setting up rel=”canonical” HTTP Header

Place the following code on the page which is near duplicate to the page white-paper.php:

//Link is used to express a typed relationship with another resource
header(“Link: <>; rel=\”canonical\””);



Example-5: Other uses of the header() function

header(“Cache-Control: max-age=3600”);
header(“Last-Modified: Tue, 15 Nov 2010 12:45:26 GMT”);
header(“If-Modified-Since: Sat, 29 Oct 2010 19:43:31 GMT”);
header(“Content-Type: text/html; charset=utf-8”);




Apache Module mod_expires

You need to have basic knowledge of this module if you want to want to make static resources (like CSS, image files etc) cacheable. Through this module you can add Expires and Cache-Control HTTP headers to static resources and thus make them cacheable.

ExpiresActive Directive
This directive enables or disable generation of Expires headers and cache-control headers for a resource like images, CSS files etc.

Syntax: ExpiresActive On|Off

Example: ExpiresActive On


ExpiresByType Directive
This directive defines the value of the Expires header and the max-age directive of the Cache-Control header generated for documents of the specified type (e.g., text/html).

Syntax: ExpiresByType MIME-type <code>seconds

Example: ExpiresByType image/gif A31104000

The second argument sets the number of seconds that will be added to a base time to construct the expiration date.

The base time is either the last modification time of the file, or the time of the client’s access to the document. Which should be used is specified by the <code> field; M means that the file’s last modification time should be used as the base time, and A means the client’s access time should be used. In the example above all GIF image files will expire after 31104000 seconds (i.e. after year) from the time of the client’s access to the document.

The Cache-Control: max-age is calculated by subtracting the request time from the expiration date and expressing the result in seconds.

ExpiresDefault Directive

This directive sets the default algorithm for calculating the expiration time for all documents in the affected realm. It can be overridden on a type-by-type basis by the ExpiresByType directive.

Syntax: ExpiresDefault <code>seconds

Example: ExpiresDefault A0

The directive in the example above set the default algorithm for calculating the expiration time for all documents from the time of the client’s access to the document to zero.



Do you want to Learn and Master Web Analytics in 8 Weeks?

I get dozens of emails every week from people asking for career advice in web analytics.

The questions range from ‘where do I start?’, ‘which books should I read?’, ‘how do I prepare for GAIQ?’ to how do I become a good analyst?

So I thought why not dedicate a whole video on answering these burning questions.

Here is what we are going to cover, in the next few minutes, in this video lesson:

#1 Who am I? What’s my story and why you should listen to me?

#2 The number 1 reason, why most marketers and business owners are not able to scale their advertising and maximize sales for themselves and/or for their clients.

#3 How in just couple of years, I went from a complete novice in web analytics to the author of three best selling books on web analytics? And how you can achieve the same transformation in your career and that too in much shorter time frame.

#4 What is the difference between web analytics and Google Analytics?

Join the Course

Take your analytics knowledge to the next level. Check out my best selling books on Amazon

Attribution Modelling in Google Ads and Facebook NEW
This book has been written to help you implement attribution modelling in Google Ads (Google AdWords) and Facebook. Learn how to leverage the knowledge of attribution modelling, understand the customer purchasing journey and determine the most effective marketing channels for investment.

Maths and Stats for Web Analytics and Conversion Optimization
This expert guide will teach you how to leverage the knowledge of maths and statistics in order to accurately interpret data and take actions, which can quickly improve the bottom-line of your online business.

Master the Essentials of Email Marketing Analytics
This book focuses solely on the ‘analytics’ that power your email marketing optimization program and will help you dramatically reduce your cost per acquisition and increase marketing ROI by tracking the performance of the various KPIs and metrics used for email marketing.

Attribution Modelling in Google Analytics and Beyond
Attribution modelling is the process of determining the most effective marketing channels for investment. This book has been written to help you implement attribution modelling. It will teach you how to leverage the knowledge of attribution modelling in order to allocate marketing budget and understand buying behaviour.

Himanshu Sharma

Certified web analyst and founder of

My name is Himanshu Sharma and I help businesses find and fix their Google Analytics and conversion issues. If you have any questions or comments please contact me.

  • Over twelve years' experience in SEO, PPC and web analytics
  • Google Analytics certified
  • Google AdWords certified
  • Nominated for Digital Analytics Association Award for Excellence
  • Bachelors degree in Internet Science
  • Founder of and

I am also the author of four books:

error: Alert: Content is protected !!