Your best bet is to use the WordPress rewrite API. No .htaccess required.
First, hook into
init and add a rewrite endpoint to your permalinks. This tells WordPress that whens some visits
/category/post-slug/gallery match the new endpoint rewrite. it also takes care of adding the query variable for you so you don't have to do that.
add_action( 'init', 'wpse27638_add_rewrite' );
add_rewrite_endpoint( 'gallery', EP_PERMALINK );
Next up we have to hack the endpoint system a bit because it doesn't automatically work whens someone visits
some-permalink/some-endpoint: your new query variable (
gallery) would only contain a value when someone visits
category/permalink/gallery/something. No good. So we hook into
request. If our new
gallery query variable is set, we'll just make it so the value is always set to true.
add_filter( 'request', 'wpse27638_request' );
function wpse27638_request( $vars )
if( isset( $vars['gallery'] ) ) $vars['gallery'] = true;
Next up, we need to hook into
the_content. If our new query var is set, we'll return a gallery shortcode in place of the content. Otherwise, we'll just return the content. You can remove your
if else statement from
single.php and just put in
the_content in its place.
add_filter( 'the_content', 'wpse27638_content_filter' );
function wpse27638_content_filter( $content )
if( ! is_singular() ) return $content;
if( get_query_var( 'gallery' ) )
return '[gallery link="file"]';
Only one finishing touch on our plugin. Add the new endpoint and flush the rewrite rules on activation. And flush the rewrite rules again on deactivation.
register_activation_hook( __FILE__, 'wpse27638_activation' );
register_deactivation_hook( __FILE__, 'wpse27638_deactivation' );
Here's the only thing as a plugin (ready to use): https://gist.github.com/1191865