We have a nice short domain name, but links to media files are way too long because the file names are long and you have the extended path for WordPress media. Like so:


You can link to the file like this:


This is nice and short (in fact this is the perfect short URL format) but this links to a template which merely contains a link to the file itself. We want a URL directly to the file itself.

Also, we don't want to use a URL shortening service as, for authority reasons, we want to be using our own domain name.

Do I need to do something with attachment.php or single-attachment.php (files I don't normally need in my themes).

1 Answer 1


It turns out this is easily solved by creating a file attachment.php and redirecting to the file itself.

  1. Create an attachment.php file in your WordPress theme folder
  2. Put this code in the file
    <?php wp_redirect(wp_get_attachment_url(), 301); ?>

  3. Upload it and there you go

This link explains in full.

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.