使 SEO 敏感 URL(避免 id) Zend 框架 [英] Make SEO sensitive URL (avoid id) Zend framework
问题描述
我有这样的网址:
http://quickstart.local/public/category1/product2
在 url (category1/product2) 中的数字是 id ,从数据库中提取的类别和产品注意 id
and in url (category1/product2) numbers are id , categorys and products fetched from database attention to the id
id 是唯一的
我需要敏感的 url,如 zend 框架 url.例如:http://stackoverflow.com/questions/621380/seo-url-structure
i need to the sensitive url like zend framework url. for example :http://stackoverflow.com/questions/621380/seo-url-structure
我如何将该网址转换为这样的新网址
how i can convert that url to the new url like this
有什么办法吗?!!
推荐答案
您可以使用 ZF 的 Zend_Controller_Router_Route.例如,要制作类似于 SO 使用的 url,可以在 application.ini 中定义一个自定义路由,如下所示(假设您有分别称为问题和显示的控制器和动作):
You could use ZF's Zend_Controller_Router_Route. For example, to make similar url to those used by SO, one could define a custom route in an application.ini as follows (assuming you have controller and action called questions and show respectively):
resources.router.routes.questions.route = '/questions/:id/:title'
resources.router.routes.questions.type = "Zend_Controller_Router_Route"
resources.router.routes.questions.defaults.module = default
resources.router.routes.questions.defaults.controller = questions
resources.router.routes.questions.defaults.action = show
resources.router.routes.questions.defaults.id =
resources.router.routes.questions.defaults.title =
resources.router.routes.questions.reqs.id = "\d+"
有了这样的路由,您可以在视图中生成如下网址:
Having such a route, in your views you could generate an url as follows:
<?php echo $this->url(array('id'=>621380,'title' => 'seo url structure'),'questions');
// results in: /myapp/public/questions/621380/seo+url+structure
//OR if you really want to have dashes in your title:
<?php echo $this->url(array('id'=>621380,'title' => preg_replace('/\s+/','-','seo url structure'),'questions');
// results in: /myapp/public/questions/621380/seo-url-structure
请注意,/myapp/public/
位于生成的 url 中,因为我没有在本地主机上设置虚拟主机,也没有对 .htaccess 进行任何修改.另请注意,您不需要具有唯一的 :title
,因为您的真实 ID 位于 :id
变量中.
Note that /myapp/public/
is in the url generated because I don't have virtual hosts setup on my localhost nor any modifications of .htaccess made. Also note that you don't need to have unique :title
, because your real id is in :id
variable.
作为旁注,如果您想让它稍微对用户更友好,最好将您的网址设为 /question/621380/see-url-structure
而不是 /questions/621380/see-url-structure
.这是因为在这个 url 下,您只有一个问题,而不是很多问题.这可以通过将路由更改为以下 resources.router.routes.questions.route = '/question/:id/:title'
来简单地完成.
As a side note, if you wanted to make it slightly more user friendly, it would be better to have your url as /question/621380/see-url-structure
rather than /questions/621380/see-url-structure
. This is because under this url you would have only one question, not many questions. This could be simply done by changing the route to the following resources.router.routes.questions.route = '/question/:id/:title'
.
如何处理问题中的类别和产品?所以,我会定义一个自定义路由,但这次使用 Zend_Controller_Router_Route_Regex:
And what to do with categories and products that you have in your question? So, I would define a custom route, but this time using Zend_Controller_Router_Route_Regex:
resources.router.routes.questions.route = '/questions/(\d+)-(d+)/(\w*)'
resources.router.routes.questions.type = "Zend_Controller_Router_Route_Regex"
resources.router.routes.questions.defaults.module = default
resources.router.routes.questions.defaults.controller = questions
resources.router.routes.questions.defaults.action = show
resources.router.routes.questions.map.1 = category
resources.router.routes.questions.map.2 = product
resources.router.routes.questions.map.3 = title
resources.router.routes.questions.reverse = "questions/%d-%d/%s"
然后将生成此路线的网址:
The url for this route would be then generated:
<?php echo $this->url(array('category' => 6213,'product' => 80,'title' => preg_replace('/\s+/', '-', 'seo url structure')),'questions' ); ?>
// results in: /myapp/public/questions/6213-80/seo-url-structure
希望这会有所帮助或至少为您指明正确的方向.
Hope this will help or at least point you in the right direction.
这篇关于使 SEO 敏感 URL(避免 id) Zend 框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!