HTACCESS 301 redirect for a URL with spaces (%20) in the slug

H

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:

https://designertofullstack.com/google-chrome-start-page-now-shows-abandoned-carts/Google%20Docs

Note the inclusion of %20 (space) in the URL.

The correct URL is:

https://designertofullstack.com/google-chrome-start-page-now-shows-abandoned-carts/

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:

  1. Change %20 to literal spaces in your URL slug intended for redirect (the first link in your Redirect 301)
  2. 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.


Notes:

  1. If you include a URL with spaces in your .htaccess file without wrapping the URL in quotes, your site will break.
  2. The order of your directives in your .htaccess file can affect redirect behavior. If you’re experiencing unexpected results, review the sequence of your directives.

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.

Happy Redirecting!

About the author

Kelly Barkhurst

Designer to Fullstack is my place to geek out and share tech solutions from my day-to-day as a graphic designer, programmer, and business owner (portfolio). I also write on Arts and Bricks, a parenting blog and decal shop that embraces my family’s love of Art and LEGO bricks!

Add comment

By Kelly Barkhurst

Recent Posts

Archives

Categories