最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

url rewriting - Getting add_rewrite_rule and add_rewrite_tag to work

programmeradmin0浏览0评论

I am new to using add_rewrite_rule and add_rewrite_tag. I am trying to get a very basic example to work, but nothing I do seems to make it work.

I want to access this URL (this works when entering this URl in directly):

http://localhost/?author_name=rewrite

Via this URL:

http://localhost/name/rewrite

I have the following code in functions.php:

$wp_rewrite->flush_rules();

add_action( 'init', 'addMyRules' );
function addMyRules(){
    add_rewrite_rule('^people/([^/]*)/?','index.php?author_name=$matches[1]','top');
    add_rewrite_tag('%author_name%','([^&]+)');
}

But I keep seeing a 404 page. Not sure what I am doing wrong.

I am new to using add_rewrite_rule and add_rewrite_tag. I am trying to get a very basic example to work, but nothing I do seems to make it work.

I want to access this URL (this works when entering this URl in directly):

http://localhost/?author_name=rewrite

Via this URL:

http://localhost/name/rewrite

I have the following code in functions.php:

$wp_rewrite->flush_rules();

add_action( 'init', 'addMyRules' );
function addMyRules(){
    add_rewrite_rule('^people/([^/]*)/?','index.php?author_name=$matches[1]','top');
    add_rewrite_tag('%author_name%','([^&]+)');
}

But I keep seeing a 404 page. Not sure what I am doing wrong.

Share Improve this question asked Mar 30, 2014 at 13:21 fakeguybrushthreepwoodfakeguybrushthreepwood 2111 gold badge5 silver badges15 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

I simply changed it to this, and it works!

add_action( 'init', 'addMyRules' );
function addMyRules(){
    add_rewrite_rule('^people/([^/]*)/?','index.php?author_name=$matches[1]','top');
    add_rewrite_tag('%author_name%','([^&]+)');
    flush_rewrite_rules();
}
发布评论

评论列表(0)

  1. 暂无评论