ScottKing.com.au

Part 1. Dynamic SEO tags for your Flash site using PHP and SWFAddress

by on Jun.10, 2008, under ActionScript, Flash, PHP, SEO, Widgets

Part 1. Dynamic SEO tags for each page of your Flash site using PHP

The method we’re going to use to serve customized SEO tags such as meta description, meta keywords, page title, noscript content and alternative content will require that we add a little PHP to our SWFAddress Flash embed page that will check what the requested page URL is and embed the relevant SEO data for that page and deeplink to the requested page in your Flash site. We will then setup a 404 page not found handeler that calls our SWFAddress and PHP enabled Flash embed page.

Utilizing the 404 handler method will allow us to setup completely custom deeplink URL’. For an example, let use a classic “About Us” page using our 404 method we will be able to use a  search engine friendly URL such as

http://www.example.com/about#about

and have that URL serve SEO tags optimised for the “About Us” page and have our .swf deeplink to the relevent content page within our Flash document. Our PHP script will detect the keyword “about” in the last part of the URL and dynamically populate the title, meta description, meta keywrods, etc for the About Us section. This means you can use even more Search Engine friendly deeplinks such as:

http://www.example.com/about.html

or

http://www.example.com/personal/about-me/about/

Once we have completed our script all 3 About Us URLs above will be working deeplinks. Which ones you choose to use and submit to Search Engines is upto you.

For this step we’ll presume that the web site resides in the root directly of your URL, however it’s easy to set it up for sites based further down in the directory structure, just substitue your webites directory structure after any root (/) references we use.

Step 1.

Setup a PHP file that will serve as the index page for your flash and that will be used for embedding your .swf, however we wont embed the swf just yet. We will however setup the dynamic title, meta description, meta keywrods, etc based on the browsers current URL.

To dynamically serve the correct SEO tags to the correct pages, first we must find out which page the user is trying to view. We will do this by detecting the URL they have in the location bar, extracting the content-page name from the URL and storing that content-page name in a variable $requestedDeepLink using the getDeepLink() function below.

<?php
function getDeepLink(){
$requestedDeepLink=curPageURL();

$requestedDeepLink = substr($requestedDeepLink,strrpos($requestedDeepLink,":80")+3);
if(strrpos($requestedDeepLink,"/")==strlen($requestedDeepLink)-1){
$requestedDeepLink = substr($requestedDeepLink,0,strlen($requestedDeepLink)-1);
}
$requestedDeepLink = substr($requestedDeepLink,strrpos($requestedDeepLink,"/"));
$exclusions = array(".html", "/","index.php"); // URL extension exclusion list
$requestedDeepLink = str_replace($exclusions, "", $requestedDeepLink); // remove extensions eg: "about.html" becomes "about"
}

function curPageURL() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}

$requestedDeepLink=getDeepLink();
echo($requestedDeepLink);
?>

Create a new PHP page and save it as index.php. Copy and paste the above code into that page and save.

Step 2.

Now we want to setup our 404 redirect function to call our new index.php script each time a 404 is detected. This means we can use our SEO friendly links such as http://www.example.com/example-only/about-me/about/ to serve the one index.php file and hence the one .swf.

This is how to setup a 404 redirect in IIS (apache .htaccess tutorial coming soon)

Open up your web site in IIS, RIght-click and go to Properties.

Right click and go to the “Custom Errors” tab.

Scroll down until you find 404 in the HTTP Error column, click it and Edit Properties

Now change Message Type to URL

And insert the abolsute path to the index.php files, if it’s in the root enter /index.php, if its in a directory enter /directory/index.php

Hit OK.

404iis 300x266 Part 1. Dynamic SEO tags for your Flash site using PHP and SWFAddress

Also, ensure that “check file exists” option is ticked in your PHP configuration in IIS while you’re here.

Hit OK again to apply the changes.

Now, if you try to browse on a non-existent URL on your website, IIS will pass the request to our index.php file and $requestedDeepLink will contain the content-page name requested in the last part of the browsers URL, for example if you browse to:

http://www.example.com/contact

contact 300x141 Part 1. Dynamic SEO tags for your Flash site using PHP and SWFAddress

You will see the content-page name “contact” is being echo’d to the screen by our script. Similarly if you change the URL to

http://www.example.com/any-SEO-friendly-url/contact-us/contact.html

you will get the same output.

contact2 300x141 Part 1. Dynamic SEO tags for your Flash site using PHP and SWFAddress

Step 3.

Great, now that we have a way to tell which content-page name has been requested by the user, or the Search Engine bot, we can serve specific content for that page.

To do this we wil setup a a few simple if statements that check the content-page name and populates some variables with our SEO tages for that specific content-page.

eg:

<?php
	function getDeepLink(){
		$requestedDeepLink=curPageURL();

		$requestedDeepLink = substr($requestedDeepLink,strrpos($requestedDeepLink,":80")+3);
		if(strrpos($requestedDeepLink,"/")==strlen($requestedDeepLink)-1){
			$requestedDeepLink = substr($requestedDeepLink,0,strlen($requestedDeepLink)-1);
		}
		$requestedDeepLink = substr($requestedDeepLink,strrpos($requestedDeepLink,"/"));
		$exclusions = array(".html", "/","index.php"); // URL extension exclusion list
		$requestedDeepLink = str_replace($exclusions, "", $requestedDeepLink); // remove extensions eg: "about.html" becomes "about"
	}

	function curPageURL() {
		$pageURL = 'http';
		if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
			$pageURL .= "://";
		if ($_SERVER["SERVER_PORT"] != "80") {
			$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
		} else {
			$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
		}
		return $pageURL;
	}

$requestedDeepLink=getDeepLink();

$title="Home";
$description="Meta Tag Description for Home";
$keywords="Meta Tag Keywords for Home";
$inPageContent="Some altenate content for non-flash users on Home";
$noscript="Some altenate content for non-javscript users on Home";
if($requestedDeepLink=="about"){
	// SEO Tags for About Me
	$title="About Me";
	$description="Meta Tag Description for About";
	$keywords="Meta Tag Keywords for About";
	$inPageContent="Some altenate content for non-flash users on About";
	$noscript="Some altenate content for non-javscript users on About";
}
if($requestedDeepLink=="contact"){
	// SEO Tags for Contact Us
	$title="Contact Us";
	$description="Meta Tag Description for Contact ";
	$keywords="Meta Tag Keywords for Contact ";
	$inPageContent="Some altenate content for non-flash users on Contact ";
	$noscript="Some altenate content for non-javscript users on Contact ";
}
?>

.

Step 4.

Now we can output those variables into our HTML page based on the requested content-page name from the requested URL.

We do this by inserting echo statements containing the SEO tags into the corresponding HTML tags like so:

<?php
	function getDeepLink(){
		$requestedDeepLink=curPageURL();

		$requestedDeepLink = substr($requestedDeepLink,strrpos($requestedDeepLink,":80")+3);
		if(strrpos($requestedDeepLink,"/")==strlen($requestedDeepLink)-1){
			$requestedDeepLink = substr($requestedDeepLink,0,strlen($requestedDeepLink)-1);
		}
		$requestedDeepLink = substr($requestedDeepLink,strrpos($requestedDeepLink,"/"));
		$exclusions = array(".html", "/","index.php"); // URL extension exclusion list
		$requestedDeepLink = str_replace($exclusions, "", $requestedDeepLink); // remove extensions eg: "about.html" becomes "about"
	}

	function curPageURL() {
		$pageURL = 'http';
		if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
			$pageURL .= "://";
		if ($_SERVER["SERVER_PORT"] != "80") {
			$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
		} else {
			$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
		}
		return $pageURL;
	}

$requestedDeepLink=getDeepLink();

$title="Home";
$description="Meta Tag Description for Home";
$keywords="Meta Tag Keywords for Home";
$inPageContent="Some altenate content for non-flash users on Home";
$noscript="Some altenate content for non-javscript users on Home";
if($requestedDeepLink=="about"){
	// SEO Tags for About Me
	$title="About Me";
	$description="Meta Tag Description for About";
	$keywords="Meta Tag Keywords for About";
	$inPageContent="Some altenate content for non-flash users on About";
	$noscript="Some altenate content for non-javscript users on About";
	$inContent=true;
}
if($requestedDeepLink=="contact"){
	// SEO Tags for Contact Us
	$title="Contact Us";
	$description="Meta Tag Description for Contact ";
	$keywords="Meta Tag Keywords for Contact ";
	$inPageContent="Some altenate content for non-flash users on Contact ";
	$noscript="Some altenate content for non-javscript users on Contact ";
	$inContent=true;
}
?>

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
	<title><?=$preTitle." ".$title?></title>
	<meta http-equiv="content-type" content="text/html; charset=utf-8" />
	<meta name="Description" content="<?=$description?>">
	<meta name="Keywords" content="<?=$keywords?>">
</head>
<body>
	<div id="flashcontent">
		<?=$inPageContent?>
		<a href="http://www.adobe.com/go/getflashplayer">Click here</a> to download the latest flash player to view this site.
	</div>
	<script type="text/javascript">
		// <![CDATA[
		//INSERT YOUR SWF OBJECT EMBED CODE HERE
		// ]]>
	</script>
	<noscript>
		<?=$noscript?>
	</noscript>
</body>
</html>

Copy and paste the above code into index.php, save and browse to it on your server again.

Now if you go to:

http://www.example.com/about

or

http://www.example.com/about.html

or

http://www.example.com/another_SEO_friendly_url/about/

And view the HTML source, you will see the title and SEO tags for the About Me page.

If you go to:

http://www.example.com/contact

or

http://www.example.com/another_SEO_friendly_url/contact-us/contact.html

And view the HTML source, you will see the title and SEO tags for the Contact Us page.

Congratulations

You can create a Google compliant sitemap.xml containing all sorts of custom SEO friendly deeplinks to all of your “pages” or deeplinks within your Flash site and you can submit your deeplinks to all the other search engines out there.

Further Customisation

You can add redirects for different file extensions other then the default “.html” by adding them to the Array at this line:
$exclusions = array(“.html”, “/”,”index.php”); // URL extension exclusion list

eg: to redirect pages that end in a .aspx extension the Array would look like:
$exclusions = array(“.html”, “/”,”index.php”,”.aspx”); // URL extension exclusion list

12 comments for this entry:
  1. Part 3. Add scrollbars for small browser windows - ScottKing.com.au

    [...] Dynamic SEO tags for your Flash site using PHP [...]

  2. Part 5: Adding Google Analytics tracking to the SWFAddres include - ScottKing.com.au

    [...] by Scott King on May.31, 2009, under Misc Dynamic SEO tags for your Flash site using PHP [...]

  3. hoolagon

    Hi Scott, I have been looking for ways to optimise my flash sites with swfAddress and your artical is just what I needed. Using a .htaccess file on Apache I have successfully linked to my custom 404 page, however I am unable to get the php working.
    I have taken a straight copy/past from your code in Step 4 and although the html part works, the variables remain set the defaults. If I use the code in Step 1, then I get nothing at all.
    Is there anything obvious that I may have missed?

    Thanks in advance,

    J

  4. Scott King

    Hi Julian,

    Not a problem, When you say the variables are set to the defaults, do you mean the SWFAddress variables in Flash arn’t being set or those in the PHP?

    You could try running an $requestedDeepLink statement anywhere after the $requestedDeepLink=getDeepLink(); call to determine if the correct variable information is being passed from the URL to the PHP.

    If you’re still having problems you could try posting the code that you’re running and Ill take a look.

  5. hoolagon

    Hi Scott,

    Thank you for your swift reply!

    I meant the variables in the PHP. ie $title, $description, $keywords etc. These never change from the original settings. ie $title=”Home”; I have not wired in the SWFAddress yet, as I would like to get the php working first.
    The code I am using is the same as yours in step 4, with one line changed $inPageContent=”404 Oops”;

    The result is tinyurl.com/mhgstj (I am hiding my domain from the spiders).

    Could it be something to do with it being on Apache rather than IIS?

    Thanks again,

    J

  6. hoolagon

    Hi Scott,

    I’ve got it working now. For some reason the line $requestedDeepLink=getDeepLink(); was not working properly. All I did in the end was to split this line into 2 steps.
    $curPageURL = curPageURL( );
    $requestedDeepLink = getDeepLink( $curPageURL );

    The full code is:

  7. hoolagon

    function getDeepLink( $deepLink ){
    $deepLink = substr($deepLink,strrpos($deepLink,”:80″)+3);
    if(strrpos($deepLink,”/”)==strlen($deepLink)-1){
    $deepLink = substr($deepLink,0,strlen($deepLink)-1);
    }
    $deepLink = substr($deepLink,strrpos($deepLink,”/”));
    $exclusions = array(“.html”, “/”,”index.php”, “.htm”); // URL extension exclusion list
    $deepLink = str_replace($exclusions, “”, $deepLink); // remove extensions eg: “about.html” becomes “about”

    return $deepLink;
    }

    function curPageURL() {
    $pageURL = ‘http’;
    if ($_SERVER["HTTPS"] == “on”) {$pageURL .= “s”;}
    $pageURL .= “://”;
    if ($_SERVER["SERVER_PORT"] != “80″) {
    $pageURL .= $_SERVER["SERVER_NAME"].”:”.$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
    } else {
    $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
    }
    return $pageURL;
    }

    $curPageURL = curPageURL( );
    $requestedDeepLink = getDeepLink( $curPageURL );

    $title=”Home”;
    $description=”Meta Tag Description for Home”;
    $keywords=”Meta Tag Keywords for Home”;
    $inPageContent=”Oops 404″;
    $noscript=”Some altenate content for non-javscript users on Home”;

  8. Scott King

    Hi Julian,

    Great work, interesting find, obviously there are some small differences in the way Apache and IIS handle their header info but it’s good to know the script also works on Apache with a small mod.

    Enjoy your SEO & Deeplinking.

  9. hoolagon

    Hi Scott,

    Thanks to you I’ve been getting into learning php which makes content creation and xhtml coding a peice of cake. One thing I have noticed however is that the googlebot does not index the pages created within the 404 php file, but instead it marks them as 404 unavailable. So for example, of the 17 pages I submitted via the sitemap.xml only 1 has been indexed, with the others all throwing crawl errors.
    This means doing a google search for something as specific as http://www.mydomain.com/portfolio does not find the submitted page http://www.mydomain.com/portfolio because it is not indexed.

    Maybe we need to actually create these pages rather than simply spoof them with the 404. What do you think?

  10. Scott King

    Hi Hoolagon,

    Good stuff, great to hear – PHP is a great little language. Once you get familiar with it you should look into a PHP framework such as CodeIgniter.

    As for Google, you’ll just need to make sure you’re not returning any 404 response codes in your header. (You can eaisly change your headers using PHP). I couldn’t get to your site to check for you (it appears to be down). Also make sure you have full links such as http://www.exmaple.com/portfolio/#portfolio in your sitemap.xml and you should also have a HTML Navigation Menu / Sitemap in the Flash alternate content in the embed DIV and again use full URL’s here.

    That should get Google properly indexing your site and deeplinking to the correct pages in your Flash movie.

    Let me know how you go.

Leave a Reply

You must be logged in to post a comment.

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...