CodeIgniter中没有使用slugs的SEO友好的URL? [英] SEO-friendly URLs in CodeIgniter without the use of slugs?
问题描述
在CodeIgniter中有一种方法(通过路由)更改:
Is there a way (via routing) in CodeIgniter to change:
example.com/category/4
到 example.com/category/foo-bar
其中 Foo Bar
是类别
4
在数据库中的名称?
where Foo Bar
is the name of category 4
in the database?
从SEO友好URL访问应允许,但通过整数访问应该导致404错误。
Access from the SEO-friendly URL should be allowed, but access via the integer should cause a 404 error.
这也应该动态,其中任何整数会自动转换为其网址安全版本的相应的类别名称。
This should also work dynamically, where any integer is automatically converted to a URL-safe version of its corresponding category name.
我见过几个使用'slugs'的解决方案...有一个不错的选择吗?
I've seen a few solutions that use 'slugs'... is there a decent alternative?
感谢。
推荐答案
我在业余时间只使用CodeIgniter过去几个月,所以我还在学习,但我会拍下这个(温柔):
I've only been working with CodeIgniter for the past couple of months in my spare time, so I'm still learning, but I'll take a shot at this (be gentle):
有一个 url_title() / code>到
foo-bar / code>函数中的URL Helper(当然需要加载) code>。
There is a url_title()
function in the URL Helper (which will need loaded, of course) that will change Foo Bar
to foo-bar
.
$name = 'Foo Bar';
$seo_name = url_title($name, TRUE);
// Produces: foo-bar
http://codeigniter.com/user_guide/helpers/url_helper.html
URL帮助程序在默认情况下剥离非法字符并抛出连字符(下划线,按参数), TRUE
参数将小写所有内容。
The URL helper strips illegal characters and throws in the hyphens by default (underscores, by parameter) and the TRUE
parameter will lowercase everything.
为了解决你的问题,我想你可以在你的routes.php文件中做一个 foreach
语句,或者传递 url_title )
值,而不是ID,并修改您的代码以使其与数据库中的类别名称匹配 url_title()
值。
To solve your problem, I suppose you could either do a foreach
statement in your routes.php file or pass the url_title()
value to the URL, rather than the ID, and modify your code to match the url_title()
value with its category name in the DB.
这篇关于CodeIgniter中没有使用slugs的SEO友好的URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!