We have custom post type as user_images and each of these post has a image attached with image name is 3 digit number followed by extension.

When the url


is accessed, we want to use a custom template inside theme to show the image

Tricky part is the url should remain same for visitor but internally should call the custom template with the wp_username and 3digitnumber as query variable. wp_username is valid WordPress user nicename.

Couldn't get a clue so made a minor compromise to url format


Tried creating a page using the custom template and used the following htaccess rule

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{QUERY_STRING} !^iuser=.*
RewriteCond %{REQUEST_URI} !^/wp-content/(.*)$ [NC]
RewriteRule ^image/(.*)/([0-9]+)$ /index.php?post_type=page&p=564&iuser=$1&iname=$2 [L]

where 564 is the page id with custom template.

However the url changes to


Also tried

function add_rewrite_rules($rules) {
    $newrules['image/([^/]+)/?([0-9]{1,})/?$'] = 'index.php?post_type=page&p=564&iuser=$matches[1]&iname=$matches[2]';

    $rules = $newrules + $rules;
    return $rules;
add_filter('rewrite_rules_array', 'add_rewrite_rules');

(called $wp_rewrite->flush_rules(); once after adding this.)

But again the url changes to


Answer: Solution provided by @Milo works perfectly. The url changed to one with query string because of Browser Cache

  • 1
    Consider using the Rewrite API instead - codex.wordpress.org/Rewrite_API and a very handy plugin that defines rewrites and helps utilizing them - wordpress.org/extend/plugins/rewrite-rules-inspector Commented Nov 20, 2012 at 22:41
  • I tried adding $newrules['image/([^/]+)/?([0-9]{1,})/?$'] = 'index.php?post_type=page&p=564&iuser=$matches[1]&iname=$matches[2]'; to rewrite_rules_array using filter. But ends up getting Memory allocation error.
    – tamilsweet
    Commented Nov 20, 2012 at 22:44
  • @MarioPeshev tried Rewrite API but i guess i'm missing something.
    – tamilsweet
    Commented Nov 20, 2012 at 22:49
  • as a side solution, you could use the template_include hook to apply a template dynamically, you have access to the $post global variable, including it's properties. I'm using that approach in a plugin of mine, the apply_remote_template function here - plugins.svn.wordpress.org/dx-template-manager/trunk/… - though it's far safer to include a file instead as you would do. Commented Nov 20, 2012 at 22:57
  • @MarioPeshev thanks for comment. Looks like even if I use the template_include hook the url may not remain same. The Rewrite API is working but the url changes to http://mysite.com/custompage/?iuser=myuser&iname=001 instead i want it to remain http://mysite.com/custompage/myuser/001
    – tamilsweet
    Commented Nov 20, 2012 at 23:01

1 Answer 1


As I mentioned in your other question, you don't want to be touching the .htaccess file, you need an internal rewrite.

So, given the URL:


we need to add the following rule to handle it:

// set up the rewrite rule
function wpa73374_setup_rewrites(){
add_action( 'init', 'wpa73374_setup_rewrites' );

Note above, we point to a page with slug custompage. You can change that to whatever page slug you are using to handle these custom URLs. If the page is a child page of another, you must add pagename=parentpage/childpage. After adding rewrite rules, be sure to visit the permalinks settings page to flush rewrites so your new rules will begin working.

Now the above alone won't work though, because WordPress doesn't know what iuser and iname are, so we need to add those to recognized query vars:

// add your custom query vars
function wpa73374_query_vars( $query_vars ){
    $query_vars[] = 'iuser';
    $query_vars[] = 'iname';
    return $query_vars;
add_filter( 'query_vars', 'wpa73374_query_vars' );

Then finally within the template, use get_query_var( 'iuser' ) and get_query_var( 'iname' ) to get your custom query var values.

  • thanks a lot for answer. Your code works but the only issue is when I go to the url http://mysite.com/image/wpusername/001 it redirects to http://mysite.com/image?iuser=wpusername&iname=001. Instead I want the url SEO friendly and unchanged. (Note I changed pagename to image itself instead of custompage)
    – tamilsweet
    Commented Nov 20, 2012 at 23:22
  • did you remove the rules you added to .htaccess?
    – Milo
    Commented Nov 20, 2012 at 23:25
  • yes I did removed the rules from .htaccess, only default WordPress rules are there. Also flushed rewrites again.
    – tamilsweet
    Commented Nov 20, 2012 at 23:26
  • There is similar question wordpress.stackexchange.com/questions/25241/… but couldn't see working answer in that as well.
    – tamilsweet
    Commented Nov 20, 2012 at 23:27
  • is image an existing page? I've tested in twentyeleven and it's working as expected. it seems like you have some conflict with htaccess or additional rules you've added elsewhere.
    – Milo
    Commented Nov 20, 2012 at 23:29

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.