Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I know there are several topics about URL rewriting in Wordpress but none of the topics I found helped me.

I want to rewrite the URL

profile/?person=abc (which works fine, but isn't pretty)

to

profile/abc

This is what I added to the functions.php of my theme:

function add_rewrite_rules() {
    global $wp_rewrite;
    add_rewrite_rule('profile/([^/]+)/?$', 'index.php?pagename=profile&person=$matches[1]', 'top');
    $wp_rewrite->flush_rules(false);
}
add_action('init', 'add_rewrite_rules');

function add_query_vars() {
    global $wp;
    $wp->add_query_var('person');
}
add_action('init', 'add_query_vars');

When I try to call

mydomain.com/profile/abc

Wordpress just changes the URL to

mydomain.com/profile

Of course 'person' is not among the query vars when I try to access them.

I also tried to write everything in one function (which shouldn't make any difference) and to add the query var 'person' to the query array with the query_vars filter hook:

function add_query_vars($q) {
    $q[] = 'person';
    return $q;
}
add_filter('query_vars', 'add_query_vars');

From everything I've read about URL rewriting with WP my code should work. I also tried deactivating all other plugins so nothing could interfere.

Solution

Since I can't answer my question because of my reputation I write the solution here:

The reason was that I tried to redirect to a child page. I left that piece of information out in my code above because I thought it was unimportant, but in fact it was the reason for my issue. Sorry for that, I just wanted to clean my code up a bit :-/

The line that got it all working:

add_rewrite_rule('persons/profile/([^/]+)/?$', 'index.php?pagename=persons/profile&person=$matches[1]', 'top');

What I don't understand is why I didn't get any error messages (WP_DEBUG was set to true) and got redirected to the right page (just without query parameter).

share|improve this question
    
have you refreshed the rewrite rules? in your code it says it is set to false, another way of doing this is by saving the permalinks again on admin > settings> permalinks even without changing anything.. –  reikyoushin Jun 7 '13 at 14:27
    
Yes, I did that. The rewrite rule itself seems to work, but it ignores the added query_var. –  Fabian Jun 7 '13 at 14:30
    
i am using the code from here: wordpress.stackexchange.com/questions/39500/… and it works.. see the function mmp_rewrite_rules and i can make the query var work. another thing, is your profile a custom post type? didn't you just add the 'query_var' => 'person' args when registering that post type? –  reikyoushin Jun 7 '13 at 14:33
    
btw i am having a problem with rewrite too.. see wordpress.stackexchange.com/questions/102246/… ;P –  reikyoushin Jun 7 '13 at 14:33
    
The filter rewrite_rules_array doesn't work for me. That was the first thing I tried but it never entered the callback function. I also found that newer tutorials and documents about rewrite rules didn't use that filter anymore. I'm using WP 3.5.1. if that makes any difference. –  Fabian Jun 7 '13 at 14:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.