Google Adwords Dynamic Keyword Insertion PHP Script

So, you want to be an Adwords rockstar?

Well then, you’ll need to wrap your brain around dynamic keyword insertion.

And I’m not just talking about the basics – just using dynamic keyword insertion in your Google Adwords advertisement. I’m talking about playing with the big kids and using those keywords that appear in your Adwords advertisement also on your web page.

That’s right, if you are targeting a specific search terms in your advertisement – make that keyword show up on your landing page. Heck, even have it show up as the title and actual url of your page. Now we are talking.

Maybe instead of talking, let me show you a few things to give you the picture. It will all be crystal clear in just a moment …

What Is Dynamic Keyword Insertion?

Lets say you are advertising using Google’s Adwords program. One of the things you provide for your ad is a list of keywords that you are interested in having your advertisement display for.

For instance, if you were selling “kobe beef”, you might want your advertisement to show up on Google’s search engine page every time someone types in the words “kobe beef” in the search box on Google.

Same goes for someone searching for “kobe steak” – you want your ad displayed.

With dynamic keyword insertion, every time someone types in the exact search term you are targeting, your advertisement would include those keyword – in bold – on your advertisement.

Here’s what the syntax looks like:

{keyword:} – will display the search term or not if there is not a match

{keyword: Default} – will display the search term or “Default” if there is problem displaying the search term. Of course, you can replace “Default” with whatever you like.

Some specific details regarding keyword: (source: snappyrank)

Kindly check out lite bet crypto

keyword – No capitalization, all word(s) are in lower case
Keyword – The first word is capitalized
KeyWord – Every word is capitalized
KEYword – Every letter in first word is capitalized
KEYWord – Every letter in the first word AND the first letter of the second
KEYWORD – Every letter is capitalized

More details at Google Adwords

How Do You Do It?

Login to your Adwords account. Create or edit a campaign. Create or edit a Text Ad.

Here’s an example of what an ad might look like that uses keyword insertion.



Notice that use of the {KeyWord: Default} in the Headline and Description line 1.

adwords keyword insertion


This is the important part for getting the search term to show up on your web site. Use the {Keyword: Default} as part of your Destination URL. In this example, I have added my top level domain, followed by the keyword insertion expression followed by “.html”. Note that I have added a dash, “-“, in between my default word.

adwords dynamic keyword insertion


Display the Adwords Search Terms On Your Page

At this point, somebody clicked on an Adwords ad of yours, something that might look like this:

adwords dynamic keyword insertion


In this case, the search term is “kobe steak”. A visitor that clicks on that advertisement would be sent to this url,

If someone searched for “kobe beef” the advertisement would look like this:

google adwords dynamic keyword insertion


On To Your Web Site

Now that you have setup your Google Adwords advertisement to use keyword insertion, it is time to separate the men from the boys, the women from the girls, the … you get the point.

Time to play big. You want your site to display an appropriate page for each of those particular search terms. Sure you can do it the old fashion way and grunt it out – create an individual page for each and every possible search term.

Or you can do it the right way.

Using PHP or some other scripting language that allows for extracting information about the referring page – the referring page that uses dynamic keyword insertion. Ding, ding, ding <<<— important

First, assuming you are working from the top level domain, i.e. –, you will need to add or modify a .htaccess file to include the following:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+).html?$ index.php?q=$1 [L]

I’m going to assume that if you are reading this far into the article that you are willing to use php files instead of plain html file (otherwise you will need to include additional code in your .htaccess file, specifically “AddType application/x-httpd-php .htm .html”).

Very simply, the chuck of code above says to turn on the rewrite engine, reference to the root directory, the rule that follows is applicable if the file or directory referenced does not already exist, then grab all the info after the domain but before the .html extension <- this is our dynamic keyword insertion stuff courtesy of Google Adwords.

Take that information (the dynamic keyword info), shove it into memory – which we will call “dollar one” = $1 and send it over to the index.php file as part of the “q” query string.

You like that techo jargon?

The PHP Code That Displays Adwords Search Terms

… on your site.

As I mentioned, you will need to be using php files for this to work (or you can modify your .htaccess file to accomodate).

At the top of your index.php file, the code should like like this:

google adwords dynamic keyword insertion


This chunk of code checks to see if any information has been passed over via the “q” query string (remember the .htaccess grabbed any info between the top level domain and the .html and sent it over as part of the query string parameter).

If there is info, then that information gets assigned to a variable called $replace (after it replaces any dashes with whitespace – assuming there are any dashes).

If no query string info is present, no problemo, there is a default value set previously – “kobe beef” in this case.

google adwords dynamic keyword insertion


Next a few other variables are set based on the what is in $replace. $title is the same as $replace with the exception that the first character of every word are capitalized. $h1 is the same as $replace except that just the first character is capatilized.

google adwords dynamic keyword insertion


Following the php script part of the index.php file is plain old html code – with the $title and $h1 variables thrown in.

google adwords dynamic keyword insertion


This is how you display the Adwords search terms on your site.

What’s Next?

We are only scratching the surface of what is possible here.

Lets just speculate that you have a datafeed that you want to create individual landing pages for that you want to use PPC to drive visitors to. Or you are a Clickbank affiliate that wants to use Adwords to promote products and you want to use the Clickbank datafeed? Or you have an Ebay store that uses a RSS feed from Ebay to display items for sale that could afford pay per click marketing?

What you have seen here is perfect for creating relevant, high quality score landing pages for using Google Awords with.

Stay tuned, or sign up to get on the mailing list, for the next article which will show you how to integrate a datafeed – like SpeedPCC – with



Very interesting information!

Question: what does adding a dash, “-”, in between your default word do for you in the destination URL. My understanding is that if the keyword fits in the destination URL field (which any google keyword would since 1024 characters are allowed), it is displayed, if not, the default text “Kobe Beef” in this example would be displayed. I understand the destination URL is NEVER displayed for the ad.

So let’s say the query is “buy kobe beef online right now”.

The destination URL would be: kobe beef online right now.html (note the spaces between words). From your experience, does google encode the destination URL with %20 for spaces?

Second, what is the purpose of putting the dash in the default destination URL DKI parameter if a) no ad will ever get directed to the default value (which would be because all keyword queries have enough room to fit in the destination URL field and b) I have not read anything indicating that if you substitute dashes for spaces in default DKI values that those spaces would be filled with dashes.

Can you explain why it is important to put dashes in the destination URL default {KeyWord:Kobe-Beef}?




Good questions.

The added “-” is overkill on my part. It just makes the urls prettier (hxxp:// versus hxxp://

The way I handle the incoming keyword, i.e. – preg_replace(“/-/”, ” “, $keyword);, will display kobe-beef.html and kobe%20beef.html the same way, “kobe beef”.




Great article. By far the most informative I seen thus far on dynamic keyword insertion.
I am trying to incorporate this into an existing website, and despite following the article fully I am unable to obtain the desired result.
I have a sub directory on my site where I wish this to work. The sub directory has an index.php file already and I copied the contents of the rewrite code into a .htaccess file which is also in the subdirectory. I note you indicate the .htaccess file should be in the root, but I am concerned it may affect existing code within the .htaccess file already in the root, if I add the rewrite code.
Can you offer any further assistance, or am I going about this completely the wrong way. I am not too concenred with the AdWords element of the article, only the section relating to displaying a page relative to the search term i.e. the /xxxx.html

Please feel free to email me if you require more information.

Regards and again thanks for a very informative article.



Hi Jeff,

From your description, it sounds like the solution is to remove:

RewriteBase /

from the .htaccess file in the subdirectory.

If that doesn’t work, use the contact link to send me more details.



UPDATE: This solution works for subdirectories. Include a .htaccess in your subdirectory that looks like this.

RewriteEngine On
RewriteBase /subdirectory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+).html?$ index.php?q=$1 [L]

Replace subdirectory with the appropriate subdirectory name


Thanks for your response.

I had another look at the various files I acquired with the original script, I had neglected to change the $_GET variable, and looking at the .htaccess file I needed to change the base variable. I now have the desired end result.

If you go to http://www.____/___/xxx.html change the xxx for any single word term, it should return an index page showing the value you used as xxx as a keyword and also in the page title. Then if you change the xxx to another value, such as kobe beef another page is displayed.
The resultant page is very basic, I will now be able to work on the rest of the design.

Paul Neal


Thanks for your post…great info. What would be a good way to test the code after making the changes to ensure that it is working properly?



Hi Paul,

If you want to be absolutely positive, then run an Adwords campaign and click on one of your ads 🙂

Otherwise, create a link to your site like this: hxxp:// and visit that page checking that “test keyword” shows up as expected.




I have followed all the instruction in using dynamic keywords in my title and line text but I can only get the default text to appear in my ad. This is occurring when I know the keyword is less than 25 spaces for the title. Do you have any suggestions?





Off hand, no.

Would you provide an example of your title? When you say you know the keyword is less than 25 spaces for the title, does that include any other content in your title?

Use the contact link if you would like to provide a specific example,




Hi Dave,
great article – many thanks.

Quick question – I take it that these dynamic pages never get indexed? They are created once and then expire (index.php?q=$1).

Would it be possible to write the user state to a static page after each referral from AdWords? This way, you would build up a group of static landing pages that would get indexed that are based on things that people are actually searching for. Could use PPC referral data for SEO in this way.




That was a great description — thanks.

I need to tweak it slightly; I want to do dynamic keyword insertion into multiple existing Wordpress pages, so I can’t use the index.html file. Obviously for each inbound Adwords visit I will know which ‘base page’ I want to be customized with the appropriate URL, and I can include this in some way in the starting URL.

How could I do this?


If you want to do dynamic keyword insertion on multiple Wordpress pages and know which page/post the visitor will land on for a particular keyword, then instead of this as your destination url:

hxxp://{KeyWord: Default}.html

You could do this:

hxxp://www.example/a-particular-page/?keyword={KeyWord: Default}

Create a short code function in wordpress that sets a cookie (or session) variable for that keyword. Then use that short code inside your page or post.

Something like this in your functions.php file for your theme:

function custom_keyword() {
if (!$isset($_COOKIE[‘keyword’])) {
setcookie(“keyword”, strip_tags(trim($_GET[‘keyword’])));
return $_COOKIE[‘keyword’];

add_shortcode(‘keyword’, ‘custom_keyword’);

P.S. – Completely winging it on this – not sure if this will work or even answers your question …

P.S.S. – Or pay for it (yes, that is an affiliate link).

Dan DelMain


Great post. Probably the best I’ve found in the short time I’ve been researching this topic.

In your response to JayTim’s question above, I have a follow up question.

I’ve created in an adwords ad with the following:
Header: {KeyWord:Portland Divorce Lawyer}
Description 1: Divorce, Child Custody & Mediation
Description 2: Don’t Get Pushed Around! Call Today
Display URL:
Destination URL:{KeyWord:Portland-Divorce-Laywer}

On the site, I’ve created a subfolder (“/legal”)- – where I want to be directing all Adwords ads with a separate htaccess file with the proper rewrites (as mentioned in your post).

But I’m having the following problem:

When I click on the Adwords ad, I get sent to the following page:{KeyWord:Portland-Divorce-Laywer}

Ideally, the Adwords would direct the visitor to the following page:

Do you know how I can either (1) Write the .htacess file to make this change or (2) amend the Destination URL in my adwords ad so it goes to the correct page?



Hi Dan,

Not 100% sure why you are landing on the {Keyword:Portlan-Divorce-Lawyer} page instead of the intended Portlan-Divocre-Laywer page.

I did notice that you are not using .html in your Destination URL.

In that case you will want to modify your .htaccess to accomodate.


RewriteRule ^([^/]+).html?$ index.php?q=$1 [L]


RewriteRule ^([^/]+)?$ index.php?q=$1 [L]


Dan DelMain


Thanks for following up so quickly.

So I switched some things around and I think I’m much closer than when I sent the post last week.

Following your advice above, it now directs to So that works.

However, when I set this up in Adwords for the destination URL, I get a weird feed. My destination URL is{Keyword:portland-divorce-lawyer}.html, which depending on the keyword/phrase being searched should end up with that exact phrase.

But instead, clicking on the ad, gives you

Is there a better way to write this in Adwords? Or does it have to do with an installed WP plugin (like All in One SEO)? Or maybe, something with the htaccess.

Thanks again. It’s making a world of difference.




Sorry, didn’t realize there is a Wordpress blog involved.

You might look into this paid option >> CLICK HERE.

EDIT: I found your ad on Google (I didn’t click the Adwords link) and noticed that you have a bunch of “extra stuff” tacked on to the destination url – that is causing a problem.

In this case, you will want to modify the .htaccess to accomodate.


RewriteRule ^([^/]+).html?$ index.php?q=$1 [L]


RewriteRule ^([^/]+).html? index.php?q=$1 [L]

i.e.- remove the dollar sign (the buck stops here).

Let me know.



Dion G

Hey Dave,

This article looked like it was exactly what I needed until I realized that it wasn’t focused on coding for Wordpress. But great content nonetheless! I tried clicking on both of the clickbank affiliate links you listed, in the article and in one of your responses above, but neither work. Would you update that link and provide it again? Thanks.

Also, being that this is a Wordpress blog, have you tried any Wordpress plugins that will dynamically generate pages with DKI urls, and title/h1/content keyword substitution? So far I’ve come across,,, and Do you have any experience with any of these? Recommendations? Thanks for your input.


Hi Dave

This is a great article, congrats! I was able tu run the code for the title of a page in my wordpress site, it is working fine there but not in the page content. When I go to the wordpress editor in HTML mode and I place the code for calling H1, the keywords do not show up in the text. Could you advice on what is going on?



Hi Laura,

What you are asking for is beyond what I showed in this post.

A Wordpress solution would require using shortcodes.

You might consider the paid resource I linked to a few comments above.

Click HERE




Great post, the best I’ve seen yet on dynamic keyword insertion.

I have implimented your suggestions and everything works axactly as it should, keywords insering into title etc. However there appears to be an issue with the URL of the landing page.

Lets use the example search term “blue widgets”.

I see the term Blue Widget’s in the sites title and where I have placed keywords in the content.

But instead of seeing the URL:
what I actually get is:

Any ideas what the issue may be?

Thanks in advance for your time.



Based on the link you are showing, it sounds like something isn’t right with your .htaccess file.


Comments on this entry are closed.

Next post: