开发者

codeigniter and seo

I'm developing an mobile phone website site using CodeIgniter. I feel in comfort with regular expression and mod_rewrite. My question is about how to create seo friendly url`s like this one in codeigniter.

http://mysite.com/phones/samsung/model_name/

I can do that using the id of categ开发者_如何学Cory, sub category etc like this.

http://mysite.com/phones_c23/samsung_s34/model_name_m32/

But i`m looking for more cleaner solution (like the example above).

Routes are unsuitable for me in that situation, because the administrator of that website can create an unlimited main categories (ex mysite.com/phones/ mysite.com/tablet/ )


Use remap function of codeigniter. The second segment of the URI typically determines which function in the controller gets called. CodeIgniter permits you to override this behavior through the use of the _remap().

What you can do is this:

IN the controller the make the function _remap(){} Now, in the remap function use the URL segment to get the fragments of your URL.

If the query is such: http://mysite.com/phones/samsung/model_name/

Your remap should find the segments "samsung" , "phones" and "model_name" .. run a query to get the details (coresponding ids etc) and then voila you have a dynamic running system.

For more information check this: http://codeigniter.com/user_guide/general/controllers.html#remapping


I wouldn't discard routing so quickly. The controller method doesn't have to be the second parameter in the url. If you take your example of what you what the URL to look like http://mysite.com/phones/samsung/ABCD/ then inside the routes.php file you can do

$route['phones/(:any)/(:any)'] = "phones/blah/$1/$2";

Inside of your "Phones" controller you would have a function called "blah" (probably good to rename that), and the phone manufacturer would be the first parameter and the phone model would be the second parameter. This would work with all manufactures and all models. You wouldn't have to have a different controller or method depending on the different combinations.

If it won't always be phones (for example it could be a tablet) and you could have a url like http://mysite.com/tablet/samsung/ABCD/, then your routes.php file would need to be something like $route['(:any)/(:any)/(:any)'] = "something/blah/$1/$2/$3"; Then the "Something" controller would have a "blah" function that would be passed electronic type (phone, tablet, etc), manufacturer, and model.

To make it even more simple, as @Stewie said, you can use _remap(). The User Guide has some good documentation and examples on how to use it http://codeigniter.com/user_guide/general/controllers.html#remapping

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜