slug怎么放到url中_如何在WordPress中更改默认搜索URL Slug

slug怎么放到url中

Do you want to change the default search URL slug in WordPress? By default, WordPress search URLs are not user friendly. In this article, we will show you how to easily change the default search url slug in WordPress and make it more SEO and user friendly.

您是否要更改WordPress中的默认搜索URL段? 默认情况下,WordPress搜索URL对用户不友好。 在本文中,我们将向您展示如何轻松地在WordPress中更改默认搜索URL插件,并使它更加SEO和用户友好。

Changing default WordPress search URL slug
为什么要在WordPress中更改默认搜索URL Slug (Why Change Default Search URL Slug in WordPress)

WordPress uses SEO friendly URL structure for all pages on your website. Typical SEO friendly WordPress URLs look like this:

WordPress对您网站上的所有页面均使用SEO友好的URL结构 。 典型的SEO友好WordPress URL如下所示:

http://example.com/some-page/
http://example.com/2018/03/some-article/
http://example.com/category/some-category/

http://example.com/some-page/
http://example.com/2018/03/some-article/
http://example.com/category/some-category/

As you can see, these URLs are quite easy to understand for users as well as search engines.

如您所见,这些URL对于用户和搜索引擎而言都很容易理解。

However, WordPress still uses non-friendly URL for search results page. A typical search URL in WordPress looks like this:

但是,WordPress仍将非友好URL用于搜索结果页面。 WordPress中的典型搜索URL如下所示:

http://example.com/?s=search-term

http://example.com/?s=search-term

This URL structure doesn’t match the rest of your website URLs. While some WordPress search plugins improve the search results, most don’t do anything about the search URL itself.

此网址结构与您网站的其余网址不匹配。 尽管某些WordPress搜索插件可以改善搜索结果,但大多数插件都不会对搜索URL本身做任何事情。

What if you can make your search URL look like:

如果您可以使搜索网址看起来像:

http://example.com/search/your-search-term/

http://example.com/search/your-search-term/

Let’s take a look at how to make the WordPress search page URL slug more SEO friendly.

让我们看一下如何使WordPress搜索页面URL标记更加SEO友好。

影片教学 (Video Tutorial)

演示地址

If you don’t like the video or need more instructions, then continue reading.

如果您不喜欢该视频或需要更多说明,请继续阅读。

在WordPress中更改搜索页面URL标记 (Changing Search Page URL Slug in WordPress)

For this tutorial you will need to edit your WordPress theme files. If you haven’t done this before, then please take a look at our guide on how to copy and paste code in WordPress.

对于本教程,您将需要编辑WordPress主题文件。 如果您以前没有做过,请查看我们的指南, 了解如何在WordPress中复制和粘贴代码

方法1.通过功能文件更改WordPress搜索URL Slug (Method 1. Change WordPress Search URL Slug via Functions File)

This method is easier and recommended for most users.

此方法更简单,建议大多数用户使用。

First, you need to copy and paste the following code in your theme’s functions.php file or a site-specific plugin:

首先,您需要将以下代码复制并粘贴到主题的functions.php文件或特定站点的插件中


function wpb_change_search_url() {
	if ( is_search() && ! empty( $_GET['s'] ) ) {
		wp_redirect( home_url( "/search/" ) . urlencode( get_query_var( 's' ) ) );
		exit();
	}	
}
add_action( 'template_redirect', 'wpb_change_search_url' );

Don’t forget to save your changes.

不要忘记保存您的更改。

You can now go to your website and try using the search feature. You will notice that the search URL looks like this:

现在,您可以转到您的网站并尝试使用搜索功能。 您会注意到搜索URL如下所示:

http://example.com/search/your-search-query/

http://example.com/search/your-search-query/

方法2。通过htaccess文件更改搜索URL Slug (Method 2. Change Search URL Slug via htaccess File)

The .htaccess file is often used to set up redirects and other website configuration tasks. You can also use it to change the default search URL slug in WordPress.

.htaccess文件通常用于设置重定向和其他网站配置任务。 您也可以使用它来更改WordPress中的默认搜索URL段。

Your .htaccess file is located in your website’s root folder, and you will need to use FTP or File Manager app in cPanel to edit it. Refer to this guide, if you can’t find the .htaccess file.

您的.htaccess文件位于网站的根文件夹中,您将需要使用cPanel中的FTP或文件管理器应用程序对其进行编辑。 如果找不到.htaccess文件 ,请参考本指南。

In your .htaccess file, you need to paste the following code at the bottom:

在您的.htaccess文件中,您需要在底部粘贴以下代码:


# Change WordPress search URL
RewriteCond %{QUERY_STRING} \\?s=([^&]+) [NC]
RewriteRule ^$ /search/%1/? [NC,R,L]

Don’t forget to save your changes and upload the .htaccess file back to the server.

不要忘记保存更改并将.htaccess文件上传回服务器。

You can now go to your website and use the search feature. You will notice the search URL will now look like this:

现在,您可以转到您的网站并使用搜索功能。 您会发现搜索URL现在将如下所示:

http://example.com/search/your-search-query/

http://example.com/search/your-search-query/

We hope this article helped you change the default search URL slug in WordPress. You may also want to see our list of 55+ most wanted WordPress tips, tricks, and hacks.

我们希望本文能帮助您更改WordPress中的默认搜索URL段。 您可能还希望查看我们的55多个最想要的WordPress技巧,窍门和hack列表

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

如果您喜欢这篇文章,请订阅我们的YouTube频道 WordPress视频教程。 您也可以在TwitterFacebook上找到我们。

翻译自: https://www.wpbeginner.com/wp-tutorials/how-to-change-the-default-search-url-slug-in-wordpress/

slug怎么放到url中

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值