在htaccess中使用正则表达式进行301重定向 [英] Using regular expression in htaccess for 301 redirects

查看:170
本文介绍了在htaccess中使用正则表达式进行301重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Wordpress博客,我在其中将博客文章从博客重定向到我的博客,格式如下:

I have a Wordpress blog where I am redirecting blog posts from my old blog to my new blog in the format below:

旧博客称为新闻,新博客简称为博客-两者都位于同一域的子目录中,如下所示。

The old blog is called 'News' and the new blog is simply called 'Blog' - both exist on the same domain in a subdirectory as indicated below.

旧的新闻博客结构

http://www.example.com/news/new-android-os-3431

新的博客博客结构

http://www.example.com/blog/new-android-os

本质上,此重定向需要做两件事:-

Essentially this redirect needs to do 2 things:-


  1. 重定向到博客目录

  2. 将帖子名称保留为相同结构,但删除URL末尾的最后一组数字

我大约需要900多个帖子才能设置重定向-我知道我可以手动添加每个帖子,但这需要一些时间。谁能指出这是否可以直接在htaccess文件中使用正则表达式来最小化此过程?

I have around 900+ posts that I need to setup redirects for - I know I could manually add each one in but that would take some time. Could anyone indicate if this could be using an regular expression directly within the htaccess file to minimise this process?

我的htaccess当前看起来像这样:

My htaccess currently looks like this:

# BEGIN WordPress
<IfModule mod_rewrite.c>
 RewriteEngine On
 RewriteBase /news/
 RewriteRule ^index\.php$ - [L]
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule . /news/index.php [L]
</IfModule>
# END WordPress


推荐答案

使用mod_alias:

Using mod_alias:

RedirectMatch 301 ^/news/(.+?)(-[0-9]+)?$ /blog/$1

或使用mod_rewrite:

or using mod_rewrite:

RewriteEngine On
RewriteRule ^news/(.+?)(-[0-9]+)?$ /blog/$1 [L,R=301]

这篇关于在htaccess中使用正则表达式进行301重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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