前几天,对blog的一番网址抒发胸臆,结果造成google大面积404,更重要的是,我自己的书签挂了好几个。不爽。 所以修改.htaccess文件,利用apache服务器的特性进行重定向。

限制:apache服务器。

当前环境: Apache/2.2.15

目标:

  1. 将文章更新,即 将所有形如"http://argcv.com/archives/1234"的修改为"http://argcv.com/articles/1234.c"

  2. 将tag,category更新,即

  • 将所有形如"http://argcv.com/archives/tag/xxx"的修改为"http://argcv.com/articles/tag/xxx"
  • 将所有形如"http://argcv.com/archives/category/xxx"的修改为"http://argcv.com/articles/tag/xxx"

做法很简单,

编辑根目录下的 .htaccess 文件,添加如下内容

# Redirect Start

RewriteEngine On
RewriteRule ^archives/(\d*)$ /articles/$1.c [R=301,L]
RewriteRule ^archives/(.*)$ /articles/$1 [R=301,L]

# Redirect End

其中,

  • "^" 表示匹配开头
  • ".*" 匹配任意长度的任意字符
  • "\d*" 匹配任意长度的数字
  • "$1" 为前面获取的第 N 个变脸, 提取方法为 () 中的内容

PS:

本来这东西是追加在.htaccess文件末尾的,可是居然没反应!怒而放到文件首部,结果就过了。 应该是其他的一些内容横插一杠子了吧。

Referenecs:


Yu

Ideals are like the stars: we never reach them, but like the mariners of the sea, we chart our course by them.

3 Comments

酷特尔 · April 15, 2013 at 12:22

Firefox 20.0 Firefox 20.0 Windows XP Windows XP

一直没弄明白301重定向有什么作用。现在我终于明白了。我博客有篇 我的404页面一文 。其中的疑惑都在这里得到解答了哈,多谢了。

Leniy · April 2, 2013 at 13:50

Google Chrome 26.0.1410.43 Google Chrome 26.0.1410.43 Windows 7 Windows 7

你当时竟然没有重定向

    yu · April 2, 2013 at 14:55

    Google Chrome 26.0.1410.43 Google Chrome 26.0.1410.43 GNU/Linux x64 GNU/Linux x64

    当时改完就闪,去做正事了

    今天翻记录出了几个404 才不爽写一下的。

Leave a Reply to yu Cancel reply

Your email address will not be published. Required fields are marked *