欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

WordPress永久链接简介

程序员文章站 2024-03-26 13:29:11
...

Website owners typically want readers to share their content either on their own sites or on social networks. To do this, readers need a link to share.

网站所有者通常希望读者在自己的网站或社交网络上共享其内容。 为此,读者需要共享链接。

什么是WordPress永久链接? (What is a WordPress Permalink?)

A ‘permalink’ is a ‘permanent link’ to a specific page or post on a website. When you share a link, you expect that it won’t change and will be a permanent resource.

“永久链接”是指向特定页面或网站上帖子的“永久链接”。 共享链接时,您希望它不会更改,并且将是永久资源。

WordPress generates a permalink for each post or page you create, which can be edited.

WordPress为您创建的每个帖子或页面生成一个永久链接,可以对其进行编辑。

There are several types of permalinks that WordPress uses. In this article, I’ll provide an introduction to permalinks, so that you’ll be able to choose the best type for your WordPress site.

WordPress使用几种类型的永久链接。 在本文中,我将对固定链接进行介绍,以便您能够为WordPress网站选择最佳类型。

默认和丑陋的永久链接 (The Default and Ugly Permalinks)

Each post or page you create has a unique identifier, called the ID. It’s a number that can uniquely identify your posts or pages, so it’s perfect for a default permalink system, and it’s the one chosen by WordPress.

您创建的每个帖子或页面都有一个唯一的标识符,称为ID。 这个数字可以唯一地标识您的帖子或页面,因此非常适合默认的永久链接系统,并且是WordPress选择的数字。

Let’s assume that you write a post that has the ID 308. Then, its default permalink will be http://example.com/?p=308. By definition, the ID is unique, so by accessing the value of the p tag contained in the URL, WordPress will know which post to retrieve.

假设您编写的ID 308的帖子。 然后,其默认永久链接将为http://example.com/?p=308 。 根据定义, ID是唯一的,因此通过访问URL中包含的p标签的值,WordPress将知道要检索的帖子。

It’s a simple system that has a big advantage: it can work on any server, so anyone can use it. However, these permalinks are ugly and not really explicit, and that’s why pretty permalinks exist.

这是一个简单的系统,具有很大的优势:它可以在任何服务器上运行,因此任何人都可以使用它。 但是,这些永久链接是丑陋的,并不是真正明确的,这就是为什么存在漂亮的永久链接的原因。

在WordPress中启用漂亮的永久链接 (Enabling Pretty Permalinks in WordPress)

Pretty permalinks (also known as search friendly URLs) typically contain several words, like the title of the post they refer to. For example, http://example.com/my-blog-post. This way, we can get an understanding of what the content of the page or post will be just by reading the URL. Furthermore, it will also be readable by search engines which in turn can help with search engine rankings.

漂亮的永久链接(也称为搜索友好URL)通常包含多个单词,例如它们所引用的帖子的标题。 例如, http://example.com/my-blog-post 。 这样,我们只要阅读URL就可以了解页面或帖子的内容。 此外,搜索引擎也可以读取它,从而可以帮助提高搜索引擎排名。

选择固定链接结构 (Choosing the Permalinks Structure)

To enable pretty permalinks, go to the Settings > Permalinks menu in your WordPress dashboard. There you’ll find several options available where you can choose for the structure of the permalinks.

要启用漂亮的永久链接,请转到WordPress信息中心中的“设置”>“永久链接”菜单。 在这里,您会找到几个可用的选项,您可以在其中选择永久链接的结构。

The ‘ugly permalinks’ option is the default configuration, with the p parameter. The other options available are ‘pretty permalinks’, you can see in the examples on-screen what information is used. If you don’t like the options WordPress offers, you can even choose the structure you want with the last option ‘Custom Structure’.

“ ugly permalinks”选项是默认配置,带有p参数。 可用的其他选项是“漂亮的永久链接”,您可以在屏幕上的示例中查看使用了哪些信息。 如果您不喜欢WordPress提供的选项,甚至可以通过最后一个选项“自定义结构”选择所需的结构。

You can add any text you want in this structure (but be sure to only use URL-friendly characters!). Some tags allow you to include details like the year of the post, or its title. These tags are called ‘structure tags‘, a term used to describe the variables used in permalinks. Note that this is not to be confused with the standard WordPress ‘tags’ that you use to help categorise your content.

您可以在此结构中添加任何文本(但请确保仅使用URL友好字符!)。 一些标签可让您包括帖子年份或标题等详细信息。 这些标签称为“ 结构标签 ”,该术语用于描述永久链接中使用的变量。 请注意,请勿将其与用于对内容进行分类的标准WordPress“标签”混淆。

Your structure can specify the date and time of the post. The year of the post is available with the %year%. Include the month with %monthnum% and the day with %day%. The time of the post is available with %hour%, %minute% and %second%.

您的结构可以指定帖子的日期和时间。 可以通过%year%获得该职位的%year% 。 在月份中包含%monthnum% ,在日期中包含%day% 。 发布时间可通过%hour%%minute%%second%

Two tags allow you to add more information about the post: the category with %category% and the author with %author%. For a post with multiple categories, only one will be included by using %category%: the first one, in alphabetical order.

有两个标签可让您添加有关帖子的更多信息:具有%category%%category%和具有%author% 。 对于具有多个类别的帖子,使用%category%只能包含一个%category% :第一个按字母顺序排列。

Finally, we find two important tags: %post_id% and %postname% which refer, respectively, to the post ID and the post title. Note that %postname% doesn’t include the exact post title: instead, it’s the slug which is included, a string representing the title, but only with URL-friendly characters.

最后,我们找到两个重要的标签: %post_id%%postname% ,它们分别引用帖子ID和帖子标题。 请注意, %postname%不包含确切的帖子标题:而是包含了slug,代表标题的字符串,但仅包含URL友好字符。

If you choose a custom structure, you must include the %post_id% or the %postname% tag or both. Without one of these tags, you can’t be sure that there will be only one post for each URL.

如果选择自定义结构,则必须包含%post_id%%postname%标记或两者。 没有这些标签之一,您将无法确保每个URL只有一篇文章。

Let’s take an absurdly long example to show what’s possible. To add the date, the time, and the author of a post, we can choose the following structure:

让我们举一个荒唐的例子来说明可行的方法。 要添加日期,时间和帖子作者,我们可以选择以下结构:

/%year%/%monthnum%/%day%/%hour%:%minute%:%second%/%postname%/written-by-%author%

and we will get URLs like the following one

我们将获得类似以下网址的网址

http://example.com/2016/08/02/08:06:22/hello-world/written-by-jeremy

分类和标签 (Categories and Tags)

Pretty permalinks are not only for your posts. In fact, they are also useful for your categories, tags and archives’ pages. If you choose any other option than ‘ugly permalinks’ for your post URLs, WordPress will automatically enable pretty permalinks for these pages.

漂亮的永久链接不仅用于您的帖子。 实际上,它们对于您的类别,标签和档案的页面也很有用。 如果您为帖子网址选择“丑陋的永久链接”以外的其他选项,则WordPress将自动为这些页面启用漂亮的永久链接。

You’ll then have URLs like http://example.com/category/uncategorized/ for your categories, instead of http://example.com/?cat=1 by default. For your tags, the tag slug will be prefixed by /tag/. For archives, you’ll get virtual directories like http://example.com/2016/08/ for instance. You can choose the prefix you want for your categories and tags, by filling the fields in the ‘Optional’ section.

然后,您将为您的类别使用类似http://example.com/category/uncategorized/ URL,而不是默认情况下的http://example.com/?cat=1 。 对于您的标签,标签slug将以/tag/作为前缀。 对于档案,您将获得虚拟目录,例如http://example.com/2016/08/ 。 您可以通过填写“可选”部分中的字段,为类别和标签选择所需的前缀。

编辑.htaccess (Editing the .htaccess)

To work, pretty permalinks uses a .htaccess file. This file must be at the root of the WordPress installation. It redirects all the visitors’ queries to the index.php file that interprets them and displays the right page.

要工作,漂亮的永久链接使用.htaccess文件。 该文件必须位于WordPress安装的根目录下。 它将所有访问者的查询重定向到index.php文件,该文件对其进行解释并显示正确的页面。

If WordPress can edit the .htaccess file itself, all you have to do is hit the ‘Save Changes’ button in Settings > Permalinks to enable pretty permalinks. However, in some cases (which depends on your hosting configuration), WordPress can’t edit it, so you’ll have to do it yourself.

如果WordPress本身可以编辑.htaccess文件,则只需单击“设置”>“永久链接”中的“保存更改”按钮,即可启用漂亮的永久链接。 但是,在某些情况下(取决于您的主机配置),WordPress无法对其进行编辑,因此您必须自己进行操作。

If this is the case, you’ll see a message at the bottom of the Permalinks page, indicating that you must edit the .htaccess file. If you don’t have a .htaccess file at the root of your WordPress installation, create it and then copy the contents of the text area WordPress gives you. If this file already exists, append the content displayed on-screen to the file using a text editor.

如果是这种情况,您将在“固定链接”页面的底部看到一条消息,指示您必须编辑.htaccess文件。 如果您在WordPress安装的根目录下没有.htaccess文件,请创建该文件,然后复制WordPress为您提供的文本区域的内容。 如果此文件已经存在,请使用文本编辑器将屏幕上显示的内容附加到文件中。

几乎漂亮的固定链接 (The Almost Pretty Permalinks)

There are some cases that disallow you to use pretty permalinks. In fact, if you can’t use .htaccess files, pretty permalinks won’t be available for you. The good news is that it doesn’t mean that you’ll have to use ugly permalinks forever.

在某些情况下,不允许您使用漂亮的永久链接。 实际上,如果您不能使用.htaccess文件,那么您将无法使用漂亮的永久链接。 好消息是,这并不意味着您将不得不永远使用丑陋的永久链接。

WordPress provides an intermediate option: the ‘almost pretty’ permalinks. These permalinks are prefixed by /index.php/. Except for this detail, they are similar to pretty permalinks, giving you the same options and tags. For instance, to use our example above (which is too big) with ‘almost pretty permalinks’, you can fill the ‘Custom Structure’ field with:

WordPress提供了一个中间选项:“几乎漂亮”的永久链接。 这些永久链接以/index.php/为前缀。 除了这个细节,它们与漂亮的永久链接相似,为您提供相同的选项和标签。 例如,要使用上面的示例(太大)使用“几乎漂亮的永久链接”,可以在“自定义结构”字段中填写:

/index.php/%year%/%monthnum%/%day%/%hour%:%minute%:%second%/%postname%/written-by-%author%

You’ll then get URLs like:

然后,您将获得如下URL:

http://example.com/index.php/2016/08/02/08:06:22/hello-world/written-by-jeremy

This is all without the need of a .htaccess file.

这一切都不需要.htaccess文件。

As for pretty permalinks, if you enable ‘almost pretty permalinks’, your categories, tags and archives pages will also get almost pretty URLs. Once again, they’ll have the same structure as the pretty permalinks, but prefixed with /index.php/.

至于漂亮的永久链接,如果启用“几乎漂亮的永久链接”,则类别,标签和档案页面也将获得漂亮的URL。 再次,它们将具有与漂亮的永久链接相同的结构,但以/index.php/为前缀。

编辑子弹头 (Editing a Post Slug)

If you use the %postname% tag in your permalinks structure, you can customize even more of your URLs. For each post, you can edit the slug that WordPress generates.

如果在永久链接结构中使用%postname%标记,则可以自定义更多URL。 对于每个帖子,您可以编辑WordPress生成的子弹。

By default, WordPress takes the title and converts it to only use URL-friendly characters. For instance, spaces are converted into dashes, and special characters are also converted, or simply removed: ‘é’ will become ‘e’, ‘à’ will become ‘a’, and ‘?’ or ‘!’ will be removed, for example.

默认情况下,WordPress使用标题并将其转换为仅使用URL友好字符。 例如,空格被转换为破折号,特殊字符也被转换或被简单地删除:“é”将变为“ e”,“à”将变为“ a”和“?” 要么 '!' 例如,将被删除。

WordPress doesn’t remove small words such as ‘the’ or ‘for’, known as ‘stop words’. These words are not very meaningful in URLs, and you may want to remove them. It’s possible to do that, post by post.

WordPress不会删除“ the”或“ for”之类的小词,即“停用词”。 这些词在URL中意义不大,您可能希望删除它们。 有可能这样做,逐个邮寄。

It’s worth mentioning that several SEO plugins automatically take care of removing stop words, so you might already have this functionality on your site. Amit Diwan has a great article reviewing the popular SEO plugins.

值得一提的是,有几个SEO插件会自动删除掉停用词,因此您的网站上可能已经具有此功能。 阿米特·迪万(Amit Diwan)有一篇很棒的文章回顾了流行的SEO插件

Below the Editor’s field for the post title you can read the generated permalink of the post. Next to this link, there’s a button labelled ‘Edit’, which allows you to edit the post permalink.

在帖子标题的“编辑者”字段下方,您可以阅读生成的帖子永久链接。 在此链接旁边,有一个标记为“编辑”的按钮,可用于编辑帖子的永久链接。

Once you edit the field with your new slug, hit the ‘Ok’ button. Note that WordPress will ensure that your new URL is valid by creating the slug using the text you entered. If you entered ‘Hello World!’, then you’ll get ‘hello-world’, for example.

使用新的子弹编辑字段后,点击“确定”按钮。 请注意,WordPress将通过使用输入的文本创建段来确保您的新URL有效。 例如,如果您输入“ Hello World!”,那么您将获得“ hello-world”。

WordPress永久链接简介

结论 (Conclusion)

Pretty (or almost pretty) permalinks are useful for readable and SEO-friendly URLs. They can contain useful information such as the date of the post or its title. These permalinks are highly customizable and you can choose any structure you want for your website.

漂亮(或几乎漂亮)的永久链接对于可读和SEO友好的URL很有用。 它们可以包含有用的信息,例如帖子的日期或标题。 这些永久链接是高度可定制的,您可以选择网站所需的任何结构。

Note that you should take your time in choosing a structure. For instance, if you change the structure for your posts, the old URLs will all produce 404 errors which would then all need to be redirected. It’s preferable not to change them for search engines, or people who have shared links to your posts on forums for example.

请注意,您应该花时间选择结构。 例如,如果您更改帖子的结构,则旧的URL都将产生404错误,然后都将需要重定向。 最好不要对搜索引擎或在论坛上共享了您的帖子链接的人进行更改。

翻译自: https://www.sitepoint.com/an-introduction-to-wordpress-permalinks/