0

I have the following rule in my htaccess file:

RewriteRule ^product/(.*)/(.*)/([0-9]+)$   product_page.php?prod=$1&code=$3&lang=$2 [QSA]

when I have / slash / within my product name -

http://www.mySite/product/namePart1/namePart2/_lang2/722 works

but

http://www.mySite/product/namePart1%2FnamePart2/_lang2/722 does not work

any hint why? I thought that translating / to %2F [using rawurlencode] will solve a problem. but it creates the problem!

Thanks,

Atara

Atara
  • 3,523
  • 6
  • 37
  • 56
  • Why the first case don't work for you? why you want to use the `%2F` syntax in your second case? – Nelson Nov 28 '12 at 08:51
  • I use rawurlencode because some of the characters in product-name are not ascii. They could be Francais accents, or Hebrew characters. – Atara Nov 28 '12 at 09:02

2 Answers2

2

I would recommend that you create some sort of slug of the product name. As far as i know its a setting in apache that'll allow you to use encoded slashes in urls.

This might help you:
http://www.jampmark.com/web-scripting/5-solutions-to-url-encoded-slashes-problem-in-apache.html

You could also easily create a slug doing something like this

function slug($phrase, $maxLength = 255) {
    $result = strtolower($phrase);
    $result = preg_replace("/[^a-z0-9\s-]/", "", $result);
    $result = trim(preg_replace("/[\s-]+/", " ", $result));
    $result = trim(substr($result, 0, $maxLength));
    $result = preg_replace("/\s/", "-", $result);

    return $result;
}
  • The link explains the problem, and adds solutions. in my case, I use - (str_replace(array('%2F','%5C'), array('%252F','%255C'), rawurlencode($strTitle))) – Atara Nov 28 '12 at 09:22
0

I would go the easy and pragmatic way of just replacing %2F back into slash after your rawurlencode() call, like this:

$nameencoded = rawurlencode($product_name);
$nameencoded = str_replace('%2F', '/', $nameencoded);
Nelson
  • 49,283
  • 8
  • 68
  • 81