I have a page created (page ID 235) with a page template that will need to be passed a variable. I would like to pass this variable through the URL. My first attempt was to pass this with a GET variable, and that worked just fine.

URL: http://example.com/sok/events/?kategori=variablehere

Now, I thought I'd spice this up a bit and prettify it. This is what I am aiming to achieve:

URL: http://example.com/sok/events/variablehere

The code I have been using in my attempt looks like the following:

function setup_filter_rewrites()
    add_rewrite_rule('^sok/events/([^/]*)/?', 'index.php?p=235&kategori=$matches[1]', 'top');
add_action( 'init', 'setup_filter_rewrites' );

Now, when accessing the URL above, I am redirected to http://example.com/sok/events/ - i.e. the URL but without the query variable. Obviously, as you may guess, the query variable is not accessible inside the page.

I heard of the redirect_canonical function potentially causing problems with rewrite rules, so I disabled it. After having done so, accessing the above URL instead yields a 404.php request.

As you can see, I've really tried to cut back on the code to make it as simple as possible as well as using the correct API functions. I have checked the rule with A tool to analyze rewrite rules? and it reports the variable as being public and accessible. I tried cutting the page down to a simple echo $wp_query->query_vars['kategori'] but that did not output anything nor did it prevent the redirection.

I suspect the redirection is the problem here - if I could stop it, I'm confident I'd be able to grab the query variable.

This problem is similar to URL Rewrite + Page + Custom Post Type = Unusual Redirect and Querystring parameter getting lost in rewrite rule insofar that it is also a redirection error. However, in my case, I am not dealing with custom post types of any kind.

1 Answer 1


I think you just have to add kategori to the array of query vars:

EDIT- after much iteration we've arrived at a solution. the original code seemed to be working, but only with specific permalink settings, as I was mistakenly routing to a post instead of a page. rather than p=, it should be pagename=, with both the parent and child page in the path:

function setup_filter_rewrites()
    add_rewrite_rule('sok/events/([^/]*)/?', 'index.php?pagename=sok/events&kategori=$matches[1]', 'top');
add_action( 'init', 'setup_filter_rewrites' );  

function setup_filter_query_vars( $query_vars )
    $query_vars[] = 'kategori';
    return $query_vars;
add_filter( 'query_vars', 'setup_filter_query_vars' );
  • I thought that was what add_rewrite_tag did? I tried your code (commenting out my add_rewrite_tag) but to no avail. It also seems the two other questions I've linked haven't been answered - this is somewhat curious!
    – Doggie52
    Commented Aug 25, 2011 at 19:44
  • make sure you flush rewrite rules each time you make a change. the above code pasted in a new install works here without a redirect, so maybe you have some other code or plugin interfering with the query.
    – Milo
    Commented Aug 25, 2011 at 19:59
  • are you absolutely totaly 100% sure this worked for you on a fresh install? Do you mind copy-pasting all of the code (including the code for the page template) you used into pastebin or something? I've disabled all of my plugins, even switched back to Twenty Eleven, and made sure to refresh my permalinks every time - it still redirects to the page URL without the appended query variable. I'm beginning to think this is some sort of bug, as I cannot see what is going or wrong or where it happens.
    – Doggie52
    Commented Aug 26, 2011 at 6:59
  • @Doggie52 - ok, I think I've figured out what may be your issue. What are your permalink settings? I can reproduce the redirect if I set permalinks to /%category%/%postname%/. any other permalink setting works except that one. here are the template files I'm using.
    – Milo
    Commented Aug 26, 2011 at 18:56
  • 1
    another thing I've noticed, if you point your rewrite to a page instead of a post it won't redirect: 'index.php?pagename=somepage&kategori=$matches[1]'. Can't speak to whether or not this is a bug or expected behaviour.
    – Milo
    Commented Aug 29, 2011 at 14:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.