diff --git a/src/url.c b/src/url.c
index eda1f54..1565dd6 100644
--- a/src/url.c
+++ b/src/url.c
@@ -1402,6 +1402,12 @@ append_uri_pathel (const char *b, const char *e, bool escaped,
   int quoted, outlen;
 
   int mask;
+
+  wchar_t *w_string, *pw;
+  int wchar_count,char_buffer_size,char_size;
+  char *char_buffer;
+#define isMBS (wchar_count<e-b && wchar_count>0 && !opt.restrict_files_nonascii)
+
   if (opt.restrict_files_os == restrict_unix)
     mask = filechr_not_unix;
   else
@@ -1426,13 +1432,38 @@ append_uri_pathel (const char *b, const char *e, bool escaped,
       b = "%2E%2E";
       e = b + 6;
     }
+#ifndef WINDOWS
+  if (strcasestr(setlocale(LC_CTYPE,NULL),"utf-8")) 
+#endif
+    {
+    wchar_count = mbstowcs(NULL,b,e-b);
+    if (isMBS) 
+      {
+        char_buffer_size=sizeof(wchar_t);
+        char_buffer=alloca(char_buffer_size);
+        w_string=alloca(wchar_count*char_buffer_size);
+        mbstowcs(w_string,b,e-b);
+      }
+    }
 
   /* Walk the PATHEL string and check how many characters we'll need
-     to quote.  */
+     to quote.  */ 
   quoted = 0;
-  for (p = b; p < e; p++)
-    if (FILE_CHAR_TEST (*p, mask))
-      ++quoted;
+  if (!isMBS) 
+    {
+      for (p = b; p < e; p++)
+        if (FILE_CHAR_TEST (*p, mask))
+          ++quoted;
+    } 
+    else 
+    {
+      for (pw = w_string; (pw - w_string) < wchar_count; pw++) {
+        if (wctomb(char_buffer,pw) == 1) {
+          if (FILE_CHAR_TEST (*char_buffer, mask))
+            ++quoted;
+        }
+      }
+    }
 
   /* Calculate the length of the output string.  e-b is the input
      string length.  Each quoted char introduces two additional
@@ -1440,29 +1471,70 @@ append_uri_pathel (const char *b, const char *e, bool escaped,
   outlen = (e - b) + (2 * quoted);
   GROW (dest, outlen);
 
-  if (!quoted)
+  if (isMBS) 
     {
-      /* If there's nothing to quote, we can simply append the string
-         without processing it again.  */
-      memcpy (TAIL (dest), b, outlen);
-    }
-  else
-    {
-      char *q = TAIL (dest);
-      for (p = b; p < e; p++)
+      if (opt.restrict_files_case == restrict_lowercase
+        || opt.restrict_files_case == restrict_uppercase) 
         {
-          if (!FILE_CHAR_TEST (*p, mask))
-            *q++ = *p;
-          else
+        char *q;
+        for (pw = w_string; pw < w_string + wchar_count; ++pw)
+          {
+            if (opt.restrict_files_case == restrict_lowercase)
+              *pw = towlower (*pw);
+            else
+              *pw = towupper (*pw);
+          }
+        }
+      if (!(quoted || opt.restrict_files_case == restrict_lowercase
+        || opt.restrict_files_case == restrict_uppercase))
+        {
+          memcpy (TAIL (dest), b, outlen);
+        } 
+        else 
+        {
+          char *q = TAIL (dest);
+          int count = 0;
+          for (pw = w_string; (pw - w_string) < wchar_count; pw++) 
             {
-              unsigned char ch = *p;
-              *q++ = '%';
-              *q++ = XNUM_TO_DIGIT (ch >> 4);
-              *q++ = XNUM_TO_DIGIT (ch & 0xf);
+              count ++;
+              if (((char_size=wctomb(char_buffer,pw)) == 1) && FILE_CHAR_TEST (*char_buffer, mask)) 
+                {
+                  unsigned char ch = *char_buffer;
+                  *q++ = '%';
+                  *q++ = XNUM_TO_DIGIT (ch >> 4);
+                  *q++ = XNUM_TO_DIGIT (ch & 0xf);
+                } 
+                else 
+                {
+                  memcpy (q,char_buffer,char_size);
+                  q+=char_size;
+                 }
             }
         }
-      assert (q - TAIL (dest) == outlen);
-    }
+    } 
+    else if (!quoted)
+      {
+        /* If there's nothing to quote, we can simply append the string
+           without processing it again.  */
+        memcpy (TAIL (dest), b, outlen);
+      }
+      else
+      {
+        char *q = TAIL (dest);
+        for (p = b; p < e; p++)
+          {
+            if (!FILE_CHAR_TEST (*p, mask))
+              *q++ = *p;
+            else
+              {
+                unsigned char ch = *p;
+                *q++ = '%';
+                *q++ = XNUM_TO_DIGIT (ch >> 4);
+                *q++ = XNUM_TO_DIGIT (ch & 0xf);
+              }
+          }
+        assert (q - TAIL (dest) == outlen);
+      }
 
   /* Perform inline case transformation if required.  */
   if (opt.restrict_files_case == restrict_lowercase
