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 know the difference between Web Analytics and Google Analytics?

99.99% of course creators themselves don’t know the difference between Web analytics, Google Analytics (GA) and Google Tag Manager (GTM).

So they are teaching GA and GTM in the name of teaching Web analytics.

They just copy each other. Monkey see, monkey do.

But Web analytics is not about GA, GTM.

It is about analyzing and interpreting data, setting up goals, strategies and KPIs.

It’s about creating strategic roadmap for your business.

Web Analytics is the core skill. Google Analytics is just a tool used to implement ‘Web Analytics’.

You can also implement ‘Web analytics’ via other tools like ‘adobe analytics’, ‘kissmetrics’ etc.

Using Google Analytics without the good understanding of ‘Web analytics’ is like driving around in a car, in a big city without understanding the traffic rules and road signs.

You are either likely to end up somewhere other than your destination or you get involved in an accident.

You learn data analysis and interpretation from Web analytics and not from Google Analytics.

The direction in which your analysis will move, will determine the direction in which your marketing campaigns and eventually your company will move to get the highest possible return on investment.

You get that direction from ‘Web analytics’ and not from ‘Google Analytics’.

You learn to set up KPIs, strategies and measurement framework for your business from ‘Web analytics’ and not from ‘Google Analytics’.

So if you are taking a course only on 'Google Analytics’, you are learning to use one of the tools of ‘Web analytics’. You are not learning the ‘Web analytics’ itself.

Since any person can learn to use Google Analytics in couple of weeks, you do no get any competitive advantage in the marketplace just by knowing GA.

You need to know lot more than GA in order to work in Web analytics and marketing field.

So what I have done, if you are interested, is I have put together a completely free training that will teach you exactly how I have been able to leverage web/digital analytics to generate floods of news sales and customers and how you can literally copy what I have done to get similar results.

Here what You'll Learn On This FREE Web Class!

1) Why digital analytics is the key to online business success

2) The number 1 reason why most marketers are not able to scale their advertising and maximize sales.

3) Why Google and Facebook ads don’t work for most businesses & how to make them work.

4) Why you won’t get any competitive advantage in the marketplace just by knowing Google Analytics.

5) The number 1 reason why conversion optimization is not working for your business.

6) How to advertise on any marketing platform for FREE with an unlimited budget.

7) How to learn and master digital analytics and conversion optimization in record time.


My best selling books on Digital Analytics and Conversion Optimization

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.

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

Himanshu Sharma

Digital Marketing Consultant and Founder of

Himanshu helps business owners and marketing professionals in generating more sales and ROI by fixing their website tracking issues, helping them understand their true customers' purchase journey and helping them determine the most effective marketing channels for investment.

He has over 12 years of experience in digital analytics and digital marketing.

He was nominated for the Digital Analytics Association's Awards for Excellence. The Digital Analytics Association is a world-renowned not-for-profit association that helps organisations overcome the challenges of data acquisition and application.

He is the author of four best-selling books on analytics and conversion optimization:

error: Alert: Content is protected !!