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 have created a custom post type called 'event' and added an event called London 2012. I want the following URL structure for a page:

local.mysite.com/london-2012/speakers

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!

share|improve this question
1  
Have you followed the page load redirect by redirect, for example, with WireShark or HttpFox? I suspect you have more than one redirect happening. –  s_ha_dum Oct 19 '12 at 3:59
1  
add comment

1 Answer

up vote 1 down vote accepted

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.

add_rewrite_rule(
    '^([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:

get_query_var('event');

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.

share|improve this answer
    
Thanks for your detailed response Brian. Acccording to Monkey Analyser it's now hitting the redirect correctly: ^([A-Za-z0-9-]+)/speakers/?$ page: speakers event: ([A-Za-z0-9-]+) HOWEVER the page it is showing isn't showing and post. –  Joe Czucha Oct 20 '12 at 2:00
    
'HOWEVER the page it is showing isn't showing' sorry a bit confused by this. Do you mean you're getting a 404 error? –  Brian Fegter Oct 20 '12 at 2:48
    
Hi Brian, I've updated the original question with my findings. Thanks again for your help. –  Joe Czucha Nov 12 '12 at 17:19
    
Forgot to accept this but this ended up being a great solution, thanks Brian. –  Joe Czucha May 6 '13 at 20:01
    
Thanks @JoeCzucha ! –  Brian Fegter May 6 '13 at 22:02
add comment

Your Answer

 
discard

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

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