使用mod_rewrite从wordpress post的url中删除年份和月份,同时保持年月和月份的永久链接结构

时间:2011-01-11 16:30:54

标签: wordpress url mod-rewrite

我一直在世界各地使用这种mod_rewrite语法,我很难构建一些工作重写规则。

情境:

在Wordpress中工作,我想从网址结构中删除“年/月”,同时保持年/月自定义永久链接激活。原因是我正在使用一个生成类别特定存档的插件,但该插件依赖于永久链接结构:

/%cateogry%/%year%/%monthnum%/%postname%

目标:

我想更改此路径:category/2010/12/post-name

到此路径:category/post-name

条件:

如果,则在网址中显示年/月,然后是斜杠后面带有字符的正斜杠(/)(例如category/2010/12/post-name) {

Remove the year/month from the url;

// intended output: category/post-name

// in this case I am directly clicking on a link.

}其他{

//leave it alone; 
//intended output: category/2010/12/


/*=======
In this case I am already in the archive section and will want the 
plugin to display the archive according to the year & category

=======*/

}

我在.htaccess文件中写了这条规则

RewriteRule ^category/[0-9]{4}/[0-9]{2}/$         news/$1 [L, NC]

任何人都可以帮忙(如果可以,请解释一下你正在做的事情,这样我就可以更熟悉它了吗?)

2 个答案:

答案 0 :(得分:2)

你的问题有点令人困惑,但让我看看我是否理解。

你想改变:

http://somehost.com/category/2011/11/some-post

为:

<webroot>/category/some-post

如果是这样的话,你几乎就在那里。在重写中,您有一个反向引用($ 1),但该反向引用未在搜索模式中的任何位置定义。为了定义反向引用,用括号括住该部分。更新规则看起来像这样(也考虑了变量类别):

RewriteRule ^(.*)/[0-9]{4}/[0-9]{2}/(.*)$ $1/$2 [L,NC]

答案 1 :(得分:-1)

转到管理员 - 设置 - 永久链接

选择自定义结构并使用

/%postname%/
相关问题