While exploring my Google Analytics, I noticed a URL users were landing on that doesn’t exist. The URL is a typo, and the page shows a 404 error. Interestingly though, the offending URL includes spaces in the slug. Space is shown as %20 in a URL address.
To ensure users get to where they intend to go, I set up a 301 Redirect for the incorrect URL.
The incorrect URL is:
Note the inclusion of %20 (space) in the URL.
The correct URL is:
Note that the correct URL does not have Google Docs or a space in the address.
Here’s the syntax for adding a 301 redirect to the .htaccess file to fix and redirect this incorrect URL.
The typical syntax for a URL redirect in a .htaccess file is relatively simple.
A .htaccess 301 redirect has three parts. Start with typing “Redirect 301.” Then, type a space and forward slash, and then the address of the old page. Do not include the domain name. You’re typing just the relative link here. Next, type a space and type the entire URL (including the domain name) for the new page.
Redirect 301 /old.html https://example.com/new.html
The URL I need to 301 redirect includes %20 or space, so I must adapt the typical syntax that I use within the .htaccess file for this 301 redirect use case.
Example of Redirect 301 for URL with Spaces
Redirect 301 "/google-chrome-start-page-now-shows-abandoned-carts/Google Docs" https://designertofullstack.com/google-chrome-start-page-now-shows-abandoned-carts/
#For the page with spaces in the URL, I first changed all %20 to spaces (literally click the space bar) and then wrapped the URL containing spaces with quotes.
And the 301 redirects work. What’s important to do is:
- Change %20 to literal spaces in your URL slug intended for redirect (the first link in your Redirect 301)
- Enclose your complete URL slug (the first link in your Redirect 301) with quotes
I hope this solves your redirect woes for 301 redirecting a single page that contains spaces in the URL.
Note: If you include a URL with spaces in your .htaccess file without wrapping the URL in quotes, your site will break.
Are you interested in learning more about the basics of domain names and especially why it’s important to have redirects for www/non-www and http/https configured correctly with your DNS and server-side scripts? Check out our detailed post about Domain Name Basics and Redirects.