@emelie
In Kohana, you can generate SEO-friendly URLs by using the URL::title method to create a URL-safe version of your string and then using the Route class to map it to a specific controller action.
Here is an example to show you how to create a SEO-friendly URL in Kohana:
This will create a URL like http://example.com/articles/my-article-title
for an article with the title "My Article Title".
Note: The Route class uses Regular Expressions to match URLs, so you can customize the pattern to match your needs. In this example, the pattern [a-zA-Z0-9_-]+
matches any combination of letters, numbers, underscores, and hyphens.
@emelie
It is important to note that generating SEO-friendly URLs is just one aspect of search engine optimization. It is also important to focus on other SEO techniques such as creating high-quality content, using relevant keywords, and optimizing your website's structure and load times.
@emelie
Additionally, it is worth mentioning that starting from Kohana 3.3, it is recommended to use the Kohana Routing module instead of manually defining routes. This module provides a powerful and flexible way to define routes and handle SEO-friendly URLs.
To use Kohana Routing module, you can follow these steps:
1 2 3 4 5 |
Kohana::modules(array( // ... 'routing' => MODPATH.'routing', // Enable Routing module // ... )); |
1 2 3 4 5 6 |
// articles route Route::set('articles', 'articles(/<title>)') ->defaults(array( 'controller' => 'Article', 'action' => 'view', )); |
In this example, the route will match URLs like http://example.com/articles
or http://example.com/articles/my-article-title
, where my-article-title
is the SEO-friendly version of the article title.
1 2 3 4 5 |
public function action_view($request) { $title = $request->param('title'); // ... your code here } |
1
|
$url = URL::to_route('articles', array('title' => $article->title)); |
This will generate the SEO-friendly URL based on the defined route.
Remember to always consider best practices for SEO when creating URLs, such as including relevant keywords, keeping URLs concise and readable, and avoiding the use of special characters or excessive parameters.