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:

http://example.com/wp-content/uploads/my-long-filename.pdf

You can link to the file like this:

http://example.com/?p=1234

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.

Leave a Reply

Your email address will not be published. Required fields are marked *