制作 SEO 敏感 URL(避免 id)Zend 框架

发布于 2024-10-27 05:27:55 字数 306 浏览 1 评论 0原文

我有这样的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

明媚殇 2024-11-03 05:27:55

您需要在数据库中存储一个唯一值,其字段名称如“url”或类似名称。每次生成新产品时,您都必须创建这个唯一的 URL 并将其与产品信息一起存储。执行此操作的常见方法是获取产品名称并使其成为 url 友好的:

public function generateUrl($name)
{
    $alias = str_replace(' ', '-', strtolower(trim($name)));
    return preg_replace('/[^A-Za-z0-9-]/', '', $alias);
}

调用此方法:

$url = $this->generateUrl("My amazing product!");
echo $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:

public function generateUrl($name)
{
    $alias = str_replace(' ', '-', strtolower(trim($name)));
    return preg_replace('/[^A-Za-z0-9-]/', '', $alias);
}

Calling this method:

$url = $this->generateUrl("My amazing product!");
echo $url;

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

烙印 2024-11-03 05:27:55

您可以使用 ZF 的 Zend_Controller_Router_Route。例如,要制作与 SO 使用的类似的 url,可以在 application.ini 中定义自定义路由,如下所示(假设您有分别名为 questions 和 show 的控制器和操作):

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+" 

有了这样的路由,在您的视图中您可以生成一个网址,如下所示:

<?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/ 位于生成的网址中,因为我没有在本地主机上设置虚拟主机,也没有对 .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

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"

然后将生成该路由的 url:

<?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

希望这会有所帮助或至少为您指明正确的方向。

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

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 following resources.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:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文