Spaces in File Names on Websites

Modern browsers can open files whose names contain spaces, so a space does not automatically make a file unavailable. However, spaces must be encoded in a URL and can cause problems in links, scripts, command-line tools, content management systems, and third-party integrations. For predictable URLs, it is better to avoid them in website file names.

Why Spaces Can Be a Problem

In a URL, a space is normally represented as %20. For example:

https://example.com/uploads/product%20photo.jpg

Browsers usually handle this conversion automatically, but other tools may not. Common problems include:

  • broken links when a path is copied, generated, or parsed incorrectly;
  • extra escaping in HTML, CSS, shell commands, and configuration files;
  • inconsistent URLs when one system uses a literal space and another uses %20;
  • less readable links in search results, analytics reports, emails, and logs;
  • errors in deployment scripts, image processors, CDNs, or legacy software.

Recommended File-Naming Rules

Use short, descriptive names made from lowercase Latin letters, numbers, and hyphens. Hyphens are usually preferable to spaces or underscores because they keep URLs readable and clearly separate words.

  • Good: product-photo.jpg
  • Acceptable: product_photo.jpg
  • Avoid: Product Photo Final #2.jpg

It is also worth avoiding URL-reserved characters such as #, ?, %, and &. Keep the original file extension, use consistent naming rules across the project, and make each name meaningful enough to identify the file without opening it.

What to Do with Existing Files

If files with spaces have already been uploaded, choose the approach that best fits the project:

  • Rename the files and update every reference. This is the cleanest long-term solution. Check HTML, CSS, JavaScript, database content, sitemaps, structured data, and social media metadata for old paths.
  • Encode the path correctly. If a file cannot be renamed, use %20 in its URL or let a URL-aware library encode each path segment. Do not apply PHP’s urlencode() to an entire URL: it is intended for query-string values and converts spaces to plus signs. For path segments, rawurlencode() is the more appropriate function.
  • Add a redirect. If the old URL has already been published or indexed, redirect it to the new file URL when the server configuration allows this. Keep the redirect in place until old links and search indexes have been updated.

After renaming files, clear relevant application and CDN caches, then test the affected pages for missing images, downloads, and other broken resources. On case-sensitive servers, Photo.jpg and photo.jpg are different files, so check letter case as well.

Rename Multiple Image Files on Windows

The following Windows batch script creates an out directory and copies common image files into it, replacing spaces with hyphens. The source files remain unchanged.

@echo off
setlocal EnableDelayedExpansion

if not exist "out" mkdir "out"

for %%F in (*.jpg *.jpeg *.png *.webp *.gif) do (
    if exist "%%F" (
        set "name=%%~nxF"
        set "newName=!name: =-!"

        if exist "out\!newName!" (
            echo Skipped: !newName! already exists
        ) else (
            copy "%%F" "out\!newName!" >nul
            echo Created: !newName!
        )
    )
)

echo Done.
pause

Save the code as a file with the .bat extension and run it from the directory that contains the images. Review the copies in the out folder before uploading them. Files that would produce duplicate names are skipped to prevent accidental overwriting.

Rename Multiple Image Files on Linux and macOS

The same operation can be performed with Bash on both Linux and macOS. This script also creates normalized copies in an out directory and leaves the source files unchanged.

#!/usr/bin/env bash

shopt -s nullglob nocaseglob
mkdir -p "out"

for file in ./*.jpg ./*.jpeg ./*.png ./*.webp ./*.gif; do
    name="${file#./}"
    new_name="${name// /-}"

    if ; then
        echo "Skipped: $new_name already exists"
    else
        cp "$file" "out/$new_name"
        echo "Created: $new_name"
    fi
done

echo "Done."

Save the script as rename-images.sh in the image directory. Then open a terminal in that directory and run:

bash rename-images.sh

The command works with the Bash version supplied by macOS as well as common Linux distributions. The nocaseglob option makes extension matching case-insensitive, so files such as PHOTO.JPG are included too. Review the files in out before uploading them.

Conclusion

Spaces in file names are valid on many systems, but they add unnecessary complexity to web URLs. A consistent lowercase naming convention with hyphens makes assets easier to link, process, cache, move, and maintain. For existing files, rename them carefully, update all references, and preserve old public URLs with redirects where necessary.