A standard Apache module mod_rewrite, which is based on a regex parser, gives you the ability to direct traffic from a page or set of pages to another. Using the .htaccess file, you can do some of the following:
 
1. Direct requests from one subfolder to another, or the primary root domain.
2. Direct requests to a subdirectory.
3. Prevent duplicate content from www and non-www, http and https.
 
A normal .htacces URL rewrite is relatively easy, but working with character encoded URLs can be tricky. This month, I found myself stumped on a project where we needed to selectively redirect URLs with character-encoded spaces to new URLs — luckily, I found a couple solutions that worked.
 
Below, you’ll find information on how to access your .htaccess file, initiate mod_rewrite, and structure your redirect request for URLs with character encoded (%20) blank spaces.
 

Why You Should Eliminate & Redirect %20 and Blank Space URLs

 
Aside from being difficult to redirect when deactivated on your site, URLs with character encoded blank spaces are not recommended by the Network Working Group, whose mission it is to produce high-quality, relevant technical documents that influence the way people design, use, and manage the internet.
 
In their report on URIs, they state:
 
Characters can be unsafe for a number of reasons. The space character is unsafe because significant spaces may disappear and insignificant spaces may be introduced when URLs are transcribed or typeset or subjected to the treatment of word-processing programs.
 
For tightened security and cleaner URLs, you might find yourself wanting to clean up legacy URLs with blank spaces — and you can do so easily using your .htaccess file.
 

Locating Your .htaccess File

 
Every .htaccess file is at the root domain of any site.
 
1. Navigate to your root domain through your file manager system.
2. If you’re using Control Panel, make sure that you select Show Hidden Files (dotfiles)
3. Select Go.
4. Search for .htaccess and select Edit.
 
Note: If you’re hosting multiple sites in one account, then make sure you’ve selected the correct .htaccess file by looking at the root domain. It should match the root domain of the website you’re working on.
 

Initiating a mod_rewrite Rule

 
In order to initiate the mod_rewrite rule you need to use a rewrite engine. You can only initiate the RewriteEngine once in the .htaccess file. To do so, add the following line to your .htaccess file if it’s not already there:
 
RewriteEngine On
 
Following RewriteEngine On, make sure that you check to see if the module has been activated in Apache, or you may cause a server error. The check must be opened and closed around your custom rewrite rule, like so:


<IfModule mod_rewrite.c>

</IfModule>

Structuring Your Rewrite Rule for Blank Spaces

 
The way in which you structure your redirect will depend on whether you’d like to redirect an individual URL or group of URLs. In other words, do you want to redirect all URLs with a space, encoded as “%20”, or to do so selectively?
 
Regardless of whether you’re redirecting one URL or many of them, the secret to redirecting blank space and %20 URLs is using the magical “escaped s” — \s. This is the regex for blank space.
 

%20 Redirects for Individual URLs

 
Now for a solution. In the example below, I am selectively redirecting the URL http://www.austinjbay.com/test%20/ to my homepage. Use this selection and replace it with your site’s values for single URL redirects.


<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_URI} "test\s"
RewriteRule .* http://www.austinjbay.com/ [R=301,L]
</IfModule>

Note: This is the most appropriate solution if you have a page that is currently ranking and getting you a fair amount of organic traffic that you don’t want to lose. Instead of redirecting it to the homepage, you can customize the landing experience with selective redirects.
 

Group Redirects for URLs with Blank Spaces

 
You can also redirect a group of pages to an individual page. Use regex and URL pattern matching to redirect large batches of URLs. Use the selection below and replace the values with your site’s.


<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_URI} \s
RewriteRule .* https://perishablepress.com/ [R=301,L]
</IfModule>

Note: This is particularly helpful if you need to redirect a group of parameter URLs with character encoding to a base URL. You could use this for an eCommerce store with filters that generate %20 or other character encoding.
 
Don’t forget to save or upload your new and improved .htacess file. Check to make sure that your redirects are working by visiting each page in your browser. The expected behavior would be for the old URL to be replaced by your new URL with a seamless redirect and content swap to the new page.