.htaccess rewrite

I have created a custom post type called ‘event’ and added an event called London 2012. I want the following URL structure for a page:


I have created a page called Speakers giving me a working /speakers url (I have Permalinks set to ‘Post name’).

Now I created a static test site and added the following to the .htaccess:

RewriteEngine On
RewriteRule ^([A-Za-z0-9-]+)/speakers?$ /speakers.php [NC,L]

This meant that if I went to local.testsite.com/something/speakers it would display the content of speakers.php whilst retaining the orignal URL. Perfect! Or so I thought…

Now, having migrated the rule across to my WordPress install (both by adding the rule in the .htaccess file directly – under the RewriteEngine on line – and through the add_rewrite_rule() function) I am getting strange behaviour.

RewriteRule ^([A-Za-z0-9-]+)/speakers/?$ /speakers?event=%1 [L]

Instead of the URL remaining the same and showing me the content of /speakers, it is actually redirecting me to the /speakers page and the URL is changing.

This is default behavior for a page in WP even without any modifications to the .htaccess file so I can only imagine that it is not matching my rule for some reason and then hitting the RewriteCond %{REQUEST_FILENAME} !-f line. Either that, or it is matching it but then continuing despite the [L] line.

Anyway I think that covers everything – if anyone has any ideas I’d really appreciate it as I’m going a bit crazy with this!

Update 1

Sorted – thanks Brian. Your solution did work but because the query variable was called ‘event’ it was getting confused and sending me to /speakers/ as a redirect. I changed it to simply be ‘e’ and it’s fine now. Thanks again!

Solutions Collecting From Web of ".htaccess rewrite"

It looks like you need to process the rewrite using query strings rather than a URI segment and pass your query through index.php. WordPress seems to be agnostic of what you’re trying to accomplish here.

Try the following rule and visit your Settings->Permalinks page to flush previous rules and add this to the stack.

    '^([A-Za-z0-9-]+)/speakers/?$', //Checked against the global $wp object - $wp->request
    'index.php?page=speakers&event=$matches[1]', //Use WP_Query params to find your page
    'top' //This will superceed the default WP rewrite stack

Make sure to register your query variable as well with WP so you can use it within $wp_query;

add_filter('query_vars', 'my_query_vars');
function my_query_vars($vars){
    $vars[] = 'event';
    return $vars;

You can then access your event query variable using the following:


For testing, I’m very fond of the Monkeyman Rewrite Analyzer plugin. It allows you to see behind the curtain of WordPress rewrites and assess which rules are matches, and how their query strings are fleshed out.