重定向WordPress中的旧URL

We recently devised a system to redirect old URLs in PHP so that you could avoid “page not found” errors (I suggest you read it before venturing further). In this article, we’ll create a similar system for WordPress, the popular PHP CMS.

最近,我们设计了一个系统来重定向PHP中的旧URL,从而避免出现“找不到页面”错误(建议您在进一步尝试之前先阅读一下)。 在本文中,我们将为流行PHP CMS WordPress创建一个类似的系统。

WordPress有何不同? (How is WordPress different?)

WordPress routes all requests through a single index.php file. Under normal circumstances, all URLs that don’t map to a physical file or folder on your server will be handled by WordPress. This includes all invalid addresses, so there’s no need to configure a 404 error document in Apache or IIS.

WordPress通过单个index.php文件路由所有请求。 通常情况下,所有未映射到服务器上物理文件或文件夹的URL都将由WordPress处理。 其中包括所有无效地址,因此无需在Apache或IIS中配置404错误文档。

There’s probably no need to worry about redirecting old URLs if you’ve been using WordPress since day one. The system is reasonably good at finding the right page, even if you change your permalink structure (Settings > Permalinks). However, if you’re migrating content from a static site or another CMS, you’ll almost certainly have URL mismatches.

如果从第一天开始使用WordPress,就可能不必担心重定向旧URL。 即使您更改了永久链接结构(“设置”>“永久链接”),该系统也相当擅长查找正确的页面。 但是,如果您要从静态站点或另一个CMS迁移内容,则几乎可以肯定URL不匹配。

更新主题 (Updating the theme)

We’re going to handle redirects within your WordPress theme. You could create a plugin, but:

我们将处理您的WordPress主题内的重定向。 您可以创建一个插件,但是:

  • the redirects are site-specific and a generic plugin would add more complexity

    重定向是针对特定站点的,而通用插件会增加更多的复杂性
  • the code is unlikely to require further updates and it’s preferable to avoid having users configure or disable it, and

    该代码不太可能需要进一步更新,因此最好避免让用户对其进行配置或禁用,并且
  • embedding it within the theme makes it more portable–you just copy theme files to the server

    将其嵌入主题中使其更具可移植性-您只需将主题文件复制到服务器

First, locate your theme files in wp-content/themes/my-theme-name. Many themes provide a 404.php file to handle “page not found” errors, but you can create one if necessary; for example:

首先,在wp-content/themes/ my-theme-name找到您的主题文件。 许多主题提供了一个404.php文件来处理“找不到页面”错误,但是您可以根据需要创建一个。 例如:

<?php get_header(); ?>

<h1>Page not found</h1>
<p>Sorry, we cannot find that page.</p>
<p>Please browse the sitemap&hellip;</p>

<ul id="sitemap">
<?php wp_list_pages('title_li='); ?>
</ul>

<?php get_footer(); ?>

You should now include redirect.php at the top of the 404.php file, for example:

现在,您应该在404.php文件的顶部包含redirect.php ,例如:

<?php include('redirect.php'); ?>
<?php get_header(); // etc...

(Note that earlier versions of WordPress may require: include(TEMPLATEPATH.'/redirect.php'); –that’ll work in the latest versions too.)

(请注意,早期版本的WordPress可能需要: include(TEMPLATEPATH.'/redirect.php');也要在最新版本中使用。)

Now create the redirect.php file in your theme folder and add the following code:

现在,在主题文件夹中创建redirect.php文件,并添加以下代码:

<?php
// current address
$oldurl = strtolower($_SERVER['REQUEST_URI']);

// new redirect address
$newurl = '';

// old to new URL map (for you to configure)
$redir = array(

	'index.html' => '/',
	'article1.html' => '/blogs/my-first-article',
	'article2.html' => '/blogs/my-second-article'

);

while ((list($old, $new) = each($redir)) && !$newurl) {
	if (strpos($oldurl, $old) !== false) $newurl = $new;
}

// redirect
if ($newurl != '') {

	header('HTTP/1.1 301 Moved Permanently');
	header("Location: $newurl");
	exit();

}
?>

The $redir URL mapping array can be configured as necessary. Alternatively, you could use a series of regular expression replacements or a database table lookup to find a valid $newurl.

可以根据需要配置$redir URL映射数组。 另外,您可以使用一系列正则表达式替换或数据库表查找来找到有效的$newurl

I hope that helps with your WordPress URL woes. If you have other tips for managing redirects, please leave your comments below.

我希望这有助于解决您的WordPress URL问题。 如果您还有其他管理重定向的技巧,请在下面留下您的评论。

翻译自: https://www.sitepoint.com/how-to-redirect-old-urls-wordpress/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值