Forum Moderators: coopster

Message Too Old, No Replies

Suggestion on ways to make SEF urls (search engine friendly urls)

Suggestion on ways to make SEF urls (search engine friendly urls)

         

zeeshan12

12:38 pm on Mar 17, 2005 (gmt 0)

10+ Year Member



Hi,

I'm trying to create search engine friendly urls for my site. The site structure looks something like this:

Category1
-Article1
-Article2
-Product1
-Sub-catetory
--Article3
--Article4
--Product2
--Quiz1
Category2
-Article5
-Quiz2

Basically, there are 2 levels of categories and there can be an Article, a Product or a Quiz at any level.

I need the URLs to look like folders.

mysite.com/category1/category2/article1/
mysite.com/category1/quiz2/
mysite.com/category1/category2/product2/

what i'm thinking to do is to pick the last bit from the url and search in the DB tables with the item name to identify if its an Article, a product, a quiz or just a sub category. obviously in this case i'll have to define unique names for all items and it also means that before displaying the page i'll have to query the DB 4 times jus to find out the type of item.

Could anyone suggest a better solution to this problem. I'ev read the forum but couldnt find any solution.

Many Thanks.

ironik

9:03 pm on Mar 17, 2005 (gmt 0)

10+ Year Member



I've started experimenting using apache's mod_rewrite engine and accessing the $_SERVER['REQUEST_URI'] variable. It works quite well, all you have to do is build something that will parse the URI into something that you can use for navigation. Here's the htaccess directive I use:


Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME}!-f
RewriteCond %{REQUEST_FILENAME}!-d
RewriteRule .* /index.php [L]

This will divert anything that isn't an existing file or directory to the index.php file which then splits the URI into parts for navigation.

The category thing you mentioned is something that I've had to deal with before. Someone else has mentioned on the forums the 'modified pre-order tree traversal' method. Basically it allows you to find a path to a node (category), or the children of a node using one single database query. Try googling it, I know sitepoint has a great article on how to use it.

zeeshan12

9:16 am on Mar 18, 2005 (gmt 0)

10+ Year Member



Hi ironik,

first of all thanks for taking the time to reply...I have used this code in the htaccess file but it doesnt seem to be working. I am using Apache 1.3.3 with PHP 4.3.10.

I get an "Internal Server Error" when ever i try to access any file from the directory.

I didnt know this whole search engine friendly url thing would be this big of a problem...:(..Please Help!

Thanks
zeeshan

Longhaired Genius

10:32 am on Mar 18, 2005 (gmt 0)

10+ Year Member



I'm not sure you're coming at this from the right direction. First you need a search-engine UNfriendly URL (but logical and understandable). Then you can transform those URLs, with mod_rewrite in .htaccess into URLs the search-engines are happy to follow. At least, that's how I would do it.

Added: Looking at your question again, I see you want to do the work in php. If you don't manage to do it you can always fall back on mod_rewrite.

zeeshan12

10:54 am on Mar 18, 2005 (gmt 0)

10+ Year Member



hmmm...yea i guess thats what i need to do. I need to start building the pages with UnFriendly urls first. (now here i'm assuming i wont have to change my existing pages to change the urls into SE friendly urls)

Longhaired Genius

11:09 am on Mar 18, 2005 (gmt 0)

10+ Year Member



Don't assume that. If you're going the mod_rewrite route, you get everything working with query-string urls (or whatever) and then change your links to search-engine frinedly and install your mod_rewrite redirects. Don't worry about it. If you plan it carefully it should be a matter of a quick search and replace in your pages.

Just take it slowly and carefully, many people have done what you are trying to do.

zeeshan12

11:38 am on Mar 18, 2005 (gmt 0)

10+ Year Member



I think i understand what you mean now. I have already started writing my scripts. Thanks for your great support

cheers,
zeeshan