Report this

What is the reason for this report?

Default space character for web safe URL

Posted on October 13, 2020

I am converting a client from AWS S3 to Spaces for her large files. There are several hundred files totaling about 500 GBs. All of the folders and file names on AWS S3 contain space characters. The default replacement on AWS for a space character in a URL is a plus sign (+), so all of her URL references in her WordPress site have plus signs in place of the space characters (e.g., https://s3.amazonaws.com/bucket/Addiction+2017/Addiction+2017.pdf). I rcloned all of the files from AWS S3 to our Spaces repository, but the default replacement character for a space on Spaces is %20, so the above URL on Spaces is https://subdomain.domain.com/Addiction 2017/Addiction +2017.pdf. I’ve been trying to use Interconnect/IT’s Search and Replace tool to do the replacements in the database, but I can’t get it to work. My next approach was to see if I could reclone the the files and tell Spaces to use a plus sign (+) instead of %20 for the web safe replacement. Can I do that? Any other ideas? I can’t change the files on AWS because they are in active use on the current website. Any help and advice is greatly appreciated.



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Heya,

Just came across this answer and decided to write some general guidelines for anyone who comes across this in the future despite the old question.

Unfortunately, there’s no current way in Spaces to change the encoding from %20 back to +. This behavior is hard-coded into the system. It’s generally recommended as per URL encoding rules to use %20 for space characters in URLs.

Beyond manually changing the URLs stored in your database (which can be risky), you might consider using a script to pull down, rename, and re-upload your files. Ensure you have proper backups and you’ve tested this on non-production data before you proceed with any mass-renaming operations.

A possible solution can be to write code to fetch the file from Spaces and serve it through a PHP script (adjust according to your application language) where it’s URL decoded before it’s sent. Here’s a rough example of how to do it:

  1. <?php
  2. $file = 'path/to/spaces/file.pdf';
  3. $decodedFile = urldecode($file);
  4. header('Content-Type: application/octet-stream');
  5. header("Content-Transfer-Encoding: Binary");
  6. header("Content-disposition: attachment; filename=\"" . basename($decodedFile) . "\"");
  7. readfile($file);
  8. ?>

Please remember to replace ‘path/to/spaces/file.pdf’ with the path to your file in Spaces.

While this solution is more of a workaround, it should allow your client to use filenames with spaces without requiring modification of the WordPress data.

For more information on working with Spaces, you can refer to our DigitalOcean Spaces documentation.

Hope that this helps!

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.