Menu DaniWeb
Log In Sign Up
  • Read
  • Contribute
  • Meet
  1. Home
  2. Programming Forum
  3. Web Development Forum
  4. Code Snippet Repository
  5. Reusable Code Snippet

Search Engine compatible links for dynamic PHP sites

20 Years Ago ReDuX 0 Tallied Votes 121 Views Share

If you have done a PHP based website, chances are you have dynamic content called by variables and so on.
As many people use a main page (index.php) and use code within that page to dictate what content is displayed, either with if or switches or whatever.
Thing is, Google and other search engines dont like these dynamic pages and doesnt index them as they would static html pages.
Following the example of daniweb, i figured it wouldnt take much to fool these engines into thinking the pages were static. So, without further a'do, Here's how to do it...
(assumes you have php installed and use 1 root page (index.php) to navigate and display the content of the site)
Note: Does not require Apache like other mod-rewrite methods. Can be used on Windows servers(IIS) with PHP installed.

apache php
<?php
/* This first bit checks to see if $PATH_INFO is seton your php install/server
which is generally is on both apache and IIS.
Then converts the /path/to/places/links to variables path=to places=links etc. */

if(isset($PATH_INFO)) {  
	$vardata = explode('/', $PATH_INFO);   
	$num_param = count($vardata); 
	if($num_param % 2 == 0) { 
		$vardata[] = ''; 
		$num_param++; 
	} 
	for($i = 1; $i < $num_param; $i += 2) { 
		$$vardata[$i] = $vardata[$i+1]; 
	} 
} 
/* Now that we have a URL handler, we just need to have the links on the pages contents
to be converted to the same style as above. e.g. http://www.domain.com/index.php/page/links
To do this we use the following functions to do the converting, and simply pass the content page
or html template to it for conversion. */

function linkMorph($link) {
	$link = explode ("?", $link); if ($link[1] != "") {
		$ndata = explode("&",$link[1]); for ($i=0;$i<count($ndata);$i++) {
			$items = explode("=",$ndata[$i]); $data .= $items[0]."/".$items[1]."/"; 
		} 
	} if ($data != "") {
		 $ret = $link[0]."/".$data; 
	} else {
		$ret = $link[0]; 
	} return $ret;
}

function contentRewrite($text) {
	$txt = preg_replace_callback("/\<a href=\"([^\"]*)\"\>/",create_function('$match', 'return "<a href=\"".linkMorph($match[1])."\">";'), $text); return $txt;
}

/* Now you would add your switch to the top of the page in question to handle the content */
switch($page){
	default:
	$template = "main.html";
	break;
	
	case "links":
	$template "links.html";
	break;
	// etc etc..
}

/* Now we just add in the index.php the following code where we want the content to be displayed
With the altered links */

$content = contentRewrite($template);
echo $content;
?>
About the Author
Member Avatar for ReDuX
ReDuX 1 Junior Poster

webhost, netadmin, sysop, whatever u call it

Member Avatar for pbridgman
pbridgman 0 Newbie Poster
19 Years Ago

very clever!

Member Avatar for fisher318
fisher318 0 Newbie Poster
18 Years Ago

Hey guys, this is EXACTLY what I am trying to get accomplished on my site. However, the problem is that I am not real knowledgeable regarding programming. I understand HTML, and I was very easily able to figure out how to install a PHP shopping cart onto the website I am developing. However, the above code doesn't specifically tell me what to do with the code or where exactly to put it. Is this an external php file that I add to the root folder? Or is this a snippet of PHP code that I add to the store.php page of my shopping cart site? If you could break this down a little bit for me it would be greatly appreciated. I have an excellent site, and would love to have friendly url names for seo purposes, because that is what I do, and also for Google Analytics tracking of the pages. It is alot easier to monitor site navigation with page names rather than ...php?=CRN204 orwhatever it puts up there.

Thanks in advance!!!;)

Member Avatar for wiltekg
wiltekg 0 Newbie Poster
15 Years Ago

If you have done a PHP based website, chances are you have dynamic content called by variables and so on.
As many people use a main page (index.php) and use code within that page to dictate what content is displayed, either with if or switches or whatever.
Thing is, Google and other search engines dont like these dynamic pages and doesnt index them as they would static html pages.
Following the example of daniweb, i figured it wouldnt take much to fool these engines into thinking the pages were static. So, without further a'do, Here's how to do it...
(assumes you have php installed and use 1 root page (index.php) to navigate and display the content of the site)
Note: Does not require Apache like other mod-rewrite methods. Can be used on Windows servers(IIS) with PHP installed.

Anyway, how to implement it into web server without include the index.php ? i think it will not work, right ? Do you have any suggestion about it ?

Edited 15 Years Ago by wiltekg because: n/a
Reply to this topic
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.

Sign Up — It's Free!
Related Topics
  • Member Avatar Search Engine Script 13
  • Member Avatar Prototype search engine peers into future 1
  • Member Avatar foreach loop in place of for loop 2
  • Member Avatar Populate form based on drop down menu selection 5
  • Member Avatar Online classifieds 3
  • Member Avatar Search multiple fields with Mysql in php 2
  • Member Avatar Link to website in seperate window 2
  • Member Avatar Preserve Polar Bears, Don't Use Google During Copenhagen Week 5
  • Member Avatar plz help me out soon 2
  • Member Avatar Wolfram Alpha goes public 1
  • Member Avatar Simple horizontal bar chart 1
  • Member Avatar Google Caught Between a Rock and a Hard Place in China 4
  • Member Avatar Regarding the CrystalReport with asp.net 0
  • Member Avatar PageRank and Its Algorithm: Explanation needed 4
  • Member Avatar PHP security: user with same IP as the server 2
  • Member Avatar Who's Serving Up the Most Malware? Google 0
  • Member Avatar A Reeeeaaly simple Hit counter 2
  • Member Avatar Google searches are getting redirected 31
  • Member Avatar generate html pages from dynamic php pages 8
  • Member Avatar could i search images in a folder using php in offline 8
Not what you need?

Reach out to all the awesome people in our web development community by starting your own topic. We equally welcome both specific questions as well as open-ended discussions.

Start New Topic
Topics Feed
Reply to this Topic
Edit Preview

Share Post

Insert Code Block

  • Forums
  • Forum Index
  • Hardware/Software
    • Recommended Topics
  • Programming
    • Recommended Topics
  • Digital Media
    • Recommended Topics
  • Community Center
    • Recommended Topics
  • Latest Content
  • Newest Topics
  • Latest Topics
  • Latest Posts
  • Latest Comments
  • Top Tags
  • Topics Feed
  • Social
  • Top Members
  • Meet People
  • Community Functions
  • DaniWeb Premium
  • Newsletter Archive
  • Markdown Syntax
  • Community Rules
  • Developer APIs
  • Connect API
  • Forum API Docs
  • Tools
  • SEO Backlink Checker
  • Legal
  • Terms of Service
  • Privacy Policy
  • FAQ
  • About Us
  • Advertise
  • Contact Us
© 2025 DaniWeb® LLC