制作 SEO 敏感 URL(避免 id)Zend 框架
我有这样的url:
http://quickstart.local/public/category1/product2
在url(category1/product2)中数字是id,从数据库获取的类别和产品注意id
id是唯一的
我需要像zend框架url这样的敏感url。例如:http://stackoverflow.com/questions/621380/seo-url-struct
我如何将该网址转换为新网址,
有什么办法吗?
i have url like this :
http://quickstart.local/public/category1/product2
and in url (category1/product2) numbers are id , categorys and products fetched from database attention to the id
id is unique
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
is there any way?!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要在数据库中存储一个唯一值,其字段名称如“url”或类似名称。每次生成新产品时,您都必须创建这个唯一的 URL 并将其与产品信息一起存储。执行此操作的常见方法是获取产品名称并使其成为 url 友好的:
调用此方法:
将输出:
my-amazing-product
您需要检查此函数的输出是否已存在于数据库,因为您将使用此值而不是 id 进行查询。
如果您也将此逻辑应用于类别,您就可以拥有易于阅读和描述性的 URL,如下所示。不过,您可能需要调整路由才能正常工作。
http://quickstart.local/public/awesome-stuff/my-amazing-product
You'll need to store a unique value in your database with a field name such as 'url' or something similar. Every time you generate a new product you will have to create this unique url and store it with the product information. A common way to do this is to take the name of the product and make it url friendly:
Calling this method:
will output:
my-amazing-product
You'll need to check that the output from this function does not already exist in the database as you will use this value to query on instead of the id.
If you apply this logic to the categories as well, you can have easily readable and descriptive urls like the one below. You may need to tweak your routing before this works correctly though.
http://quickstart.local/public/awesome-stuff/my-amazing-product
您可以使用 ZF 的 Zend_Controller_Router_Route。例如,要制作与 SO 使用的类似的 url,可以在 application.ini 中定义自定义路由,如下所示(假设您有分别名为 questions 和 show 的控制器和操作):
有了这样的路由,在您的视图中您可以生成一个网址,如下所示:
请注意,
/myapp/public/
位于生成的网址中,因为我没有在本地主机上设置虚拟主机,也没有对 .htaccess 进行任何修改。另请注意,您不需要具有唯一的:title
,因为您的真实 ID 位于:id
变量中。顺便说一句,如果您想让它对用户更加友好,最好将您的网址设置为
/question/621380/see-url-struct
而不是/questions/ 621380/参见-url-结构
。这是因为在此网址下您只会有一个问题,而不是很多问题。只需将路由更改为以下resources.router.routes.questions.route = '/question/:id/:title'
即可轻松完成此操作。编辑:
如何处理您问题中的类别和产品?因此,我将定义一个自定义路由,但这次使用 Zend_Controller_Router_Route_Regex:
然后将生成该路由的 url:
希望这会有所帮助或至少为您指明正确的方向。
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):
Having such a route, in your views you could generate an url as follows:
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.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 followingresources.router.routes.questions.route = '/question/:id/:title'
.EDIT:
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:
The url for this route would be then generated:
Hope this will help or at least point you in the right direction.