I need to rewrite all attachment urls by the following rule

This is the current urls for the attachments:

This is how it should look like:

This is what i tried - the rule itself is working but the links don't work:

add_action('generate_rewrite_rules', 'attachment_rewrite_rule_14924');

//URL Rewrite Rule for attachments
function wpd_attachment_link( $link, $post_id ){
    $post = get_post( $post_id );
    $post = $post->post_title;
    $post = str_replace(" ","-",$post);
    return home_url( '/bilder/' . $post);
add_filter( 'attachment_link', 'wpd_attachment_link', 20, 2 );

Can anyone help me and tell what could be possibly wrong here? As i mentioned before, i get successfully the url rewritten but the url is broken then, it doesn't refer to the attached media.


Take a look on Pretty links feature of wordpress: https://codex.wordpress.org/Using_Permalinks#Using_.22Pretty.22_permalinks

This feature allow wordpress to rewrite the URL's on the fly. But may need some modifications on the host .htaccess file.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy