Title: Attachment &#8211; Modifying Permalink Rewrites
Last modified: August 20, 2016

---

# Attachment – Modifying Permalink Rewrites

 *  [letusrise](https://wordpress.org/support/users/letusrise/)
 * (@letusrise)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/attachment-modifying-permalink-rewrites/)
 * Attachment – Modifying Permalink Rewrites
 * Good day! I cannot find any documentation regarding this matter. I would like
   to modify my attachment permalink. Instead of pointing to domain.com/post_slug/
   attachment_slug/ to domain.com/attachment/attachment_slug/. Is this possible?
 *     ```
       /* add new rewrite rule */
       function attachment_rewrite( $wp_rewrite ) {
           $rule = array(
               'media/(.+)' => 'index.php?attachment=' . $wp_rewrite->preg_index(1)
           );
   
           $wp_rewrite->rules = $rule + $wp_rewrite->rules;
       }
       add_filter( 'generate_rewrite_rules', 'attachment_rewrite' );
   
       /* redirect standard wordpress attachments urls to new format */
       function redirect_old_attachment() {
           global $wp;
   
           if( !preg_match( '/^media\/(.*)/', $wp->request ) && isset( $wp->query_vars['attachment'] ) ) {
               wp_redirect( site_url( '/media/' . $wp->query_vars['attachment'] ) , 301 );
           }
       }
       add_filter( 'template_redirect', 'redirect_old_attachment' );
       ```
   
 * I already have this but the problem is. There are two ways to visit an attachment
   page.
 * Like for example:
    domain.com/this-is-a-post/this-is-an-attachment/ – When you
   visit this link you will be redirected to the link below. I want this to be disabled
   and I want it to point to this link rather domain.com/media/this-is-an-attachment/.
 * I read this is possible in the hook of attachment_link but I don’t have any idea.
   To be honest, my knowledge for permalink rewrites is limited. I really need your
   help guys.
 * Thanks and God Bless!

Viewing 1 replies (of 1 total)

 *  Thread Starter [letusrise](https://wordpress.org/support/users/letusrise/)
 * (@letusrise)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/attachment-modifying-permalink-rewrites/#post-2362000)
 * Anyone?

Viewing 1 replies (of 1 total)

The topic ‘Attachment – Modifying Permalink Rewrites’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 1 reply
 * 1 participant
 * Last reply from: [letusrise](https://wordpress.org/support/users/letusrise/)
 * Last activity: [14 years, 7 months ago](https://wordpress.org/support/topic/attachment-modifying-permalink-rewrites/#post-2362000)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
