User Jan wrote me with the following query about optimizing database driven websites.
Hi there,
I have a question about search engine friendly sites in regard to your article here about SEO of Database-driven Websites. I’m trying to make a website search engine friendly, using descriptive urls. Currently every link to a subpage looks like this: www.dom.ain/?visit=cat
So I only have 1 index file that provides information the user wants. Is there any way to improve this without creating a new php for every page on the site?
I thought about creating a *.htm that will route to the corresponding php url.
E.g. company.htm file routes to /?visit=comp
But I gathered that search engines don’t like links that are opened via Java Script.
Thx for any help, it would be greatly aprechiated.
Kind Regards, Jan
I replied:
When we have a website that uses variables in the domain names, we usually
use a system called Mod Rewrite on a Linux/Apache/mySQL/PHP (LAMP) server
to make more user-friendly URLs. This doesn’t work the same a Windows IIS
server, but works well on a LAMP server. Most websites nowadays are on LAMP
servers.
It works like this:
In the .htaccess file put these lines for each of the pages of the site.
(Contact a web programmer for a quote on doing this if it is beyond your
technical skills — it should be fairly fast and cheap to get done.)
—-
RewriteEngine On
RewriteRule ^airport.php /community.php?unit=airport.php&language=english [L]
—–
You only need to tell it “RewriteEngine On” once. For every page you want to rewrite like this, you add another line.
So when someone goes to airport.php, what they actually get is the community.php page, with all its variables. What Google “sees” is airport.php. What a visitor “sees” in his browser address bar is airport.php.
What’s actually displaying to both Google and your visitor is
community.php?unit=airport.php&language=english
This allows you to have a shorter URL that is more search engine friendly.
Here’s a better example:
RewriteRule ^big-green-widgets.php /product-display.php?productid=widgets&productstyle=blue&productsize=big[L]
There IS no page named big-green-widgets.php on the server. (Like Neo learns in The Matrix movie, “There is no spoon.”)
The server sees a request for the page “big-green-widgets.php” and because of this line in the .htaccess file, the server goes and gets the info it
needs to create the page on-the-fly.
Also, don’t use JavaScript for your links. In your navigation menus, link to the names of the pages you set up in the .htacess file, like “big-green-widgets.php“. Even if that actually mod rewrites over to www.dom.ain/?visit=cat1324, it doesn’t matter. As far as Google and your visitors are concerned, you have a page called big-green-widgets.php.
Mod rewrite is a wonderful tool for anyone trying to optimize a site that is built using only a single page with variables, or using a long string of variables and a database.
Make sense?
Best,
Jeré Matlock
http://www.wordsinarow.com
Website Design & Marketing / SEO

Wow thanks a lot for your reply, not used to getting feedback this fast, and such excellent one too.
This is pretty much exactly what I was looking for. I guess any further information, for example on dynamic generation of the .htaccess files
should be available via google.
Thanks again,
-Jan
Thanks for the info, but I have a different question more database related. I would like to have the url like this;
domain.com/products/category/product-title
category and product-title with be tables in the database. How can I get it to search the database for the proper category and product?
How could I get the .htaccess file to forward the variables to the PHP page,which can search for the category and product-title, then generate the page?
Thanks in advance,
Jim
If you want to have your website using page names something like this:
www. hubcaps.com /plymouth/ 66-valiant-hubcapsthat’s fairly easy to set up using a RewriteRule in .htaccess
RewriteEngine OnRewriteRule ^plymouth/66-valiant.php /display.php?category=plymouth&year=66&model=valiant [L]
This is great. Thank you.
It obviously allows people to get routed to the right part of your database but how does a search engine know what to look for? Does it spider the .htaccess file?
No – Google never sees the .htaccess file (it’s inaccessible through normal browsing).
Instead, when Google requests the page, in much the same fashion as a person browsing the page would request it, the server creates it “on the fly” and gives it to Google based on the action of Google requesting the page. It’s fast and it works. This is how to show elegant, search-engine-friendly URLs while having the actual content come from a database by using question marks, product ids, category ids, and so on. In practice, it works great!
hi
i have any dynamic page like this in my site:
http : / /mirsoft.net/detail.php?siteid=433how can i change to this :
http : // mirsoft.net/detail-433.htmlby .htaccess file.thanks.
Hi, Abbas -
Test this code in your .htaccess file, to see if it works:
RewriteEngine On
RewriteRule ^detail-433.htm /detail.php?siteid=433 [L]
—————-
Here are some tutorials on using .htaccess that you may find useful:
http://softexpert.wordpress.com/2007/10/15/learn-htaccess-in-10-minutes/
http://www.thejackol.com/htaccess-cheatsheet/
http://www.javascriptkit.com/howto/htaccess.shtml
Good luck!
Best,
Jere Matlock
Hi Jere.
I must be missing something. The page (http://www.jmblog.com/2009/11/15/using-htaccess-to-make-search-engine-friendly-urls-there-is-no-spoon/) contains no actual Blog post but only 7 replies. I’d really like to see what you wrote in the first place but I can’t find it. Maybe it’s just my old eyes, but . . . ??? Can you help?
Thanks
Jim
Thanks for pointing it out – broken by a blog plugin malfunctioning. I’ve repaired the problem.
Hello, thanx for the nice tut but.. could you help me out here?
I had a url which was
http : // www . spotter.gr/cat.php?catid=4and i put following in the htaccess file:
RewriteEngine OnRewriteRule ^katigories/autokinisi /cat.php?catid=4 [L]
now, when i access the page
http : // www . spotter.gr/katigories/autokinisii get a screwed up page..
i am using relative paths for my includes etc and it looks like it now searches for them in this url…
Can u help me out here???
Thanks in advance, Greetings from Greece
Jim
Hi, Jim -
I’ve run into this before.
Try setting the “base” HTML command within cat.php.
Or try setting up a Rewrite Rule that sends the “include” request to the correct folder.
Note: You can have multiple .htaccess files within the different folders on your server at different levels on the server.
Best,
Jere Matlock
Hi
Great tut, but all the time I’m reading your post and Q&A I have a question I still am very keen to confirm.. lets take the following case for example;
————————————–
Hi, Abbas -
Test this code in your .htaccess file, to see if it works:
RewriteEngine On
RewriteRule ^detail-433.htm /detail.php?siteid=433 [L]
————————————–
so the above solution is just for id number 433… does that mean we have to do this for every id 1 to 433? And what about the future where it is quite possible for ids to go up all the way to thousands.. and what if the website gets 10 new ids an hour, do we manually create a mod rewrite for each new id? Wouldn’t that take just take up all of our time?
Hi, Abbas -
No, you don’t have to do this by hand for each entry.
When you have a very large, very active database-driven site, it is best to set up something we have given the name of “dispatcher”.
Basically, you route the traffic through to a php file called dispatcher.php, that does a lookup in a mySQL database and provides the appropriate name.
We just did this for a big media room (call it media.website.com) with several thousand pages, which we routed over to a blog (blog.website.com). Most of the pages of the media room were moved, but not all of them. Requests for those pages that were not moved are served a generic page with a 301 code. Requests for pages that were moved are given a 301 redirect to the exact page name in the blog that we want to serve up instead.
To give credit where it is due, we “lifted” this system from the xCart shopping cart, which uses a file called “dispatcher.php” to do exactly the same thing. That’s how it makes search-engine-friendly URLs.
Dispatcher.php, when called by the htaccess file, becomes part of the process of determining the page name to be displayed. The .htaccess file sends all traffic to it, it does a lookup in the database, finds the right file name, and sets the “location”. It’s much, much faster than trying to do all this redirection through the .htaccess file.
So the thing to do is make a search-engine-friendly URL for any page that you create (even if it is done automatically) and store that in a database table, then use the dispatcher.php file to go get that file name and serve it up instead of the funky variable-filled URL that is not friendly to the search engines.
If you need more help on this, we would be happy to consult with you on it. Contact me directly for a free initial consultation.
could you please give a code sample for dispatcher and htaccess as ho how this is implemented?
Hi, Eric -
If you need to set this up, you need someone to do it who knows what they are doing. It’s not for the faint of heart.
Here’s an example: We had a “media. website. com” sub-domain, which was being hosted on another server. We consolidated hosting and moved all of the “media” content over to a blog, at “blog. website. com”
In the “media” folder, we put an .htaccess with this code in it:
Options +FollowSymLinksAddType text/html .html
AddType text/html .htm
AddType text/xml .xml
AddHandler server-parsed .html
AddHandler server-parsed .htm
AddHandler server-parsed .xml
RewriteEngine On
RewriteCond %{HTTP_HOST} ^media.website.com
RewriteCond %{REQUEST_URI} !^/dispatcher.php
RewriteRule (.*) /dispatcher.php?file=$1 [R=301,L,QSA]
That’s what does the “heavy lifting” for us.
——————–
Then we have in “dispatcher.php” the following code to pull apart the pieces of the URI being requested, and look for it in the little table in the database with the old and new page names in it, and then set the location. The result is a clean shift from one URL to another, with a 301 code sent and the right URI being served for the request. Note that this replaces about 400 lines of .htaccess file.
$request = $_SERVER[REQUEST_URI];
$request = trim($request);
$uriparts = explode('?', $request);
$vars = explode('&', $uriparts);
if ($uriparts[1])
{
parse_str($uriparts[1], $vars);
}
$file = array_shift($vars);
$var = array_shift($vars);
$pagevars = "";
foreach ($var as $key => $value)
{
$pagevars .= "&$key=$value";
}
$pagevars = str_replace(" ","+", $pagevars);
$pagevars = substr_replace($pagevars,"", 0,1);
$query="SELECT * FROM redirs
WHERE page = '/$file'
AND qry = '$pagevars'
LIMIT 1";
echo "
Query = $query
";
$result = mysql_query($query);
/*if (mysql_num_rows($result) == 1)
{
$row = mysql_fetch_array($result);
$redirect = "http : // blog .thiswebsite .com" . $row[direct];
header("Location: $redirect");
}
else {
header("Location: http : / /blog. thiswebsite. com/default-page.php");
} */
this is really wonderful and good and clarity explanation about the ht access. thank you for sharing.
http://www.seoupdates-sarala.blogspot.com
KUDOS!!!
I have looked for how to do what you describe in your last comment for 3 weeks with no luck. I never though it would be as easy as routing through a PHP script! I was trying to figure out a way to get .htaccess to read a database ( hits forehead with palm ).
Thanks a lot!!
Hi, Calvin -
Yes, we went that route, too. We tried for a week to get our .htaccess file to do a lookup (which it is supposed to be able to do) but we could NOT get it to work on our server, despite involving Tier II tech support and calling in an outside “expert”. Nothing doing! So we had to regroup. They say “Necessity is the mother of invention” but I think it is more like “Necessity is the mother of circumvention” in this case.
Best,
Jere
Hey,
I am working in .net … Could you please help me how can I make the search engine friendly URLs without making HTML pages for them please
..IF you find any answer then please mail me at:
Archanazohns@gmail.com
Hi, Archana – I have no idea how to do this in .net. Not my area of expertise at all. I would be very surprised if it is possible in .net.
Best of luck to you