基于类别名称的 SEO URL,URL 中没有类别 id [英] SEO URL based on category name without have a category id in URL

查看:46
本文介绍了基于类别名称的 SEO URL,URL 中没有类别 id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个表 posts &类别

post_id |post_title |post_content |post_cat--------------------------------------------------1 Hello World 欢迎来到我的.. 1.…………

categories 表格

cat_id |猫名|cat_parent-----------------------------1 新闻 NULL2 运动 1.……

假设新闻的当前类别链接是 http://domain.com/category/1/

MySQL 语句

选择posts.post_id,post.post_id,post.post_title,post.post_content,post.post_cat,类别.cat_id,category.cat_name,category.cat_parent发件人INNER JOIN 类别ON posts.post_cat = Categories.cat_idWHERE posts.post_cat = (int)$_GET['cat_id']

所以我们可以得到post_cat = 1的结果

根据我当前的数据库结构,如何删除ID 但将其更改为一个不错的 slug?示例:-

主要类别 - http://domain.com/category/news/子类别 - http://domain.com/category/news/sports/

让我知道脚本如何告诉 News 等于 1post_cat 列上的线索?

解决方案

您可以使用 .htaccess 文件 为您重写网址.

.htaccess 文件中的条目看起来像这样:

RewriteEngine onRewriteCond $1 ^categoryRewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-d重写规则 ^(.*)$ parseCategoryUrl.php?q=$1 [L,QSA]

我将在这里分解行以帮助理解发生了什么以及每行的含义:

  1. 打开重写引擎.
  2. 这种重写只会发生在主机名后的第一个单词是 category 的 URL 上 - 所有以该单词开头的 URL 都将由下面的行处理.
  3. 该规则将排除指向实际文件的 URL !-f
  4. 该规则将排除指向实际目录!-d
  5. 的URL
  6. 实际规则将捕获整个 request_uri 并将其传递给 parseCategoryUrl.php 文件,并将整个请求作为 $_GET 中名为 q (query) 的参数大批.规则末尾的标志 (L,QSA) 做两件事.
    • L - 这是最后一条规则.处理完这一行后,.htaccess 文件将停止对当前 URL 执行操作.
    • QSA - 将查询字符串附加到重写的 URL(以便我们稍后解析).

您的 parseCategoryUrl.php 文件可能包含类似于以下内容的内容:

$request = expand('/',$_GET['q']);array_shift($request);

第一行将通过斜杠将请求拆分成一个数组——第二行从数组的开头删除单词 category(因为我们知道我们正在解析一个类别 URL).

最终的 $request 数组,带有一个 URL 示例,例如:http://example.domain.com/category/news/sports
将是这样的:

数组([0] =>消息[1] =>运动的)

所以你在这里看到你现在已经成功地将 URL 拆分成一个数组;您现在要做的就是查询您的数据库并向用户提供正确的页面.
由于 parseCategoryUrl.php 页面实际上没有输出,您可以使用 include() 函数根据提供的 URL 插入正确的页面.><小时>

SEO 的目的是让您的页面及其 URL 对搜索引擎提供更多信息,以便搜索互联网的用户能够收到与其搜索查询相关的页面结果.查看 URL 的搜索引擎:http://domain.com/category/1/2将无法提取很多信息.但是,如果您的网址包含(如您的问题所要求的)类别信息,那么搜索引擎将能够推断出特定网址与以下内容相关:

  • http://domain.com/category/news/ - 新闻
  • http://domain.com/category/news/sports - 体育新闻
  • http://domain.com/category/blog/ - 博客
  • 等等...

I have two table posts & categories

post_id | post_title  | post_content   | post_cat
--------------------------------------------------
1         Hello World  welcome to my..   1
.         ..           ..                ..

categories table

cat_id | cat_name | cat_parent
-----------------------------
1        News       NULL
2        Sports     1
.        ...        .. 

Let's say current category link for news is http://domain.com/category/1/

MySQL statment

SELECT posts.post_id,
       posts.post_id,
       posts.post_title,
       posts.post_content,
       posts.post_cat,
       categories.cat_id,
       categories.cat_name,
       categories.cat_parent
FROM   posts
       INNER JOIN categories
         ON posts.post_cat = categories.cat_id  
       WHERE posts.post_cat = (int)$_GET['cat_id']

So we can get a result for post_cat = 1

According to my current database structure, how do I remove the ID but change it to be a nice slug? Example :-

Main category - http://domain.com/category/news/
Sub category  - http://domain.com/category/news/sports/

Let me know a clue how script will tell News is equal 1 on post_cat column?

解决方案

You can use an .htaccess file to rewrite the URL's for you.

The entry in the .htaccess file would look something like this :

RewriteEngine on
RewriteCond $1 ^category 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ parseCategoryUrl.php?q=$1 [L,QSA]

I'll break down the lines here to help understand whats going on and what each line means :

  1. Turn the RewriteEngine on.
  2. This rewrite will only occur on URL's where the first word after the host name is category - all URL's starting with that word will be processed by the lines below.
  3. The rule will exclude URL's that point to actual files !-f
  4. The rule will exclude URL's that point to actual directories !-d
  5. The actual rule will capture the entire request_uri and pass it to the parseCategoryUrl.php file with the entire request as a parameter called q (query) in the $_GET array. The flags at the end of the rule (L,QSA) do two things.
    • L - This is the last rule. After processing this line the .htaccess file will stop performing actions on the current URL.
    • QSA - Append the query string to the rewritten URL (so that we can parse it later).

Your parseCategoryUrl.php file could contain something similar to the following :

$request = explode('/',$_GET['q'] );
array_shift($request);

The first line will split the request by slashes into an array - and the second line removes the word category from the beginning of the array (because we know that we are parsing a category URL).

The final $request array, with a URL example such as : http://example.domain.com/category/news/sports
Will be this :

Array
(
    [0] => news
    [1] => sports
)

So you see here that you have now successfully split the URL into an array; All you have to do now is query your database and and provide the user with the correct page.
Because the parseCategoryUrl.php page in fact has no output, you could just use an include() function to insert the correct page according to the URL that was provided.


SEO is about making your pages and their URL's more informative to search engines so that a user searching the internet will be able to receive page results that are related to their search query. A search engine looking at the URL : http://domain.com/category/1/2 will not be able to extract much information. However if your URL contains (as your question requires), category information, then a search engine will be able to deduct that the specific URL is related to :

  • http://domain.com/category/news/ - news
  • http://domain.com/category/news/sports - sports news
  • http://domain.com/category/blog/ - blog
  • etc...

这篇关于基于类别名称的 SEO URL,URL 中没有类别 id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆