Using .htaccess to make search engine-friendly URLs – “There is no spoon”
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







November 15th, 2009 at 5:40 pm
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
December 8th, 2009 at 1:40 pm
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
December 8th, 2009 at 3:03 pm
If you want to have your website using page names something like this:
http://www.hubcaps.com/dodge/66-valiant-hubcaps
that’s fairly easy to set up using a RewriteRule in .htaccess
RewriteEngine On
RewriteRule ^dodge/66-valiant.php /display.php?category=dodge&year=66&model=valiant [L]
February 19th, 2010 at 12:33 am
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?
February 20th, 2010 at 11:19 pm
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!
February 23rd, 2010 at 6:00 am
hi
i have any dynamic page like this in my site:
http://mirsoft.net/detail.php?siteid=433
how can i change to this : http://mirsoft.net/detail-433.html by .htaccess file.
thanks.
February 23rd, 2010 at 10:39 am
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
June 9th, 2010 at 9:59 am
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
June 10th, 2010 at 12:49 am
Thanks for pointing it out – broken by a blog plugin malfunctioning. I’ve repaired the problem.