Pretty URLs for File Uploads?

Attachments in general (images, but also files like PDFs) have a special “attachment page” of the form example.com/main-post/attachment/file-title/. In most themes this is used to display a larger version of the image, still in the theme layout (so it returns a HTML file with the image in it, not the image directly).

You could exploit this by creating a template file that does serve the file directly, instead of just displaying it. Then if you create a page example.com/whitepapers/ with an attachment called file.php, the URL of the file will be example.com/whitepapers/attachment/file.

You would need to create a file in your theme directory called either attachment.php (if you want to do this for all attachments) or application.php (if you only want to do this for files with a mime type like application/*). In that file you get the attachment file location and then readfile() it to the browser. I guess there are already plugins out there that can do this for you.

Leave a Comment