phil
phil

Reputation: 51

How to rewrite urls that have an accidental %20 at the end?

I wound up with a bunch of backlinks to my site that have the correct URL, except there was an accidental trailing space so the links came out with a trailing %20, which causes a page not found error.

I tried variations on this:

RewriteRule ^/%20 /

but it's not working.

Is a RewriteCond statement also needed?

Please note this is an IIS 6 server, and these are Wordpress pages that I'm linking to.

Somebody please tell he the secret code to get rid of a %20 at the end of a URL.

Thanks!

Upvotes: 4

Views: 1210

Answers (1)

marapet
marapet

Reputation: 56466

You may permanently redirect all concerned urls having a trailing %20 to the same url without the trailing %20 by using the following rule:

If you use UrlDecoding Off in your iirf.ini, use:

RedirectRule (.*)%20$ $1 [R=301]

Otherwise, IIRF automatically decodes the URL for you before trying to apply the rules. You can therefore use:

RedirectRule (.*)\s$ $1 [R=301]

In order to test this using testdriver.exe:

  1. Put the above rule in a file called iirf.ini.
  2. Create a file called SampleUrls.txt containing some test URLs, for example:

    /                      NO REWRITE
    /%20                   REDIRECT 301 /
    /article               NO REWRITE
    /article%20            REDIRECT 301 /article
    
  3. Call testdriver with a command similar to %iirfpath%\testdriver.exe -d .

Please note: testdriver does not decode URLs.

You should get an output similar to the following (I removed some newlines):

TestDriver: linked with 'Ionic ISAPI Rewriting Filter (IIRF) 2.1.1.28 x64 RELEASE'.
TestDriver: The IIRF library was built on 'Aug  8 2011 02:26:29'

Processing URLs...(.\SampleUrls.txt)

***
Retrieving server variable that is not supported by TestDriver (SCRIPT_NAME)

NO REWRITE '/' ==> --
OK

***
Retrieving server variable that is not supported by TestDriver (SCRIPT_NAME)

REDIRECT 301 '/%20' ==> '/'
OK

***
Retrieving server variable that is not supported by TestDriver (SCRIPT_NAME)

NO REWRITE '/article' ==> --
OK

***
Retrieving server variable that is not supported by TestDriver (SCRIPT_NAME)

REDIRECT 301 '/article%20' ==> '/article'
OK


0 Errors in 4 Total Trials

Upvotes: 2

Related Questions