WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I changed attachment permalink by add_filter('attachment_link','my_media_url'); from




The change is successful. But, with the new URL, the page is not found. Some suggest rewrite rules like: this

'media/(.+)' => 'index.php?attachment=' . $wp_rewrite->preg_index(1)

I tried, it doesn't work in my case. Instead of modify patterns before or after "attachment_id", I want to replace "attatchment_id" with "media" and keep all the other stuffs unchanged. Is this a question of rewrite, or should I study something else? I tried this, not working either:

function attachment_rewrite( $wp_rewrite ) {
     $rule = array(
         '/?media=' => 'index.php?attachment_id=' . $wp_rewrite->preg_index(1)

    $wp_rewrite->rules = $rule + $wp_rewrite->rules; } add_filter( 'generate_rewrite_rules', 'attachment_rewrite' );
share|improve this question
Does this help? wordpress.stackexchange.com/a/14931/10691 – its_me Jul 2 '12 at 4:01
No, that rule remove "?" and modify "_id" to "/id". I want to keep "?" and modify"attachment_id" to "media" – Jenny Jul 2 '12 at 4:21
Hmm... I meant, modify that code to your needs. Can you try this and see if it works? pastebin.com/8715TTVi – its_me Jul 2 '12 at 5:01
Thanks for the code! But it can't find the page either. – Jenny Jul 2 '12 at 5:22
"Compilation failed: nothing to repeat at offset 1 in ....\wp-includes\class-wp.php on line 201" – Jenny Jul 2 '12 at 5:31

Your Answer


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

Browse other questions tagged or ask your own question.