Atara
Atara

Reputation: 3569

friendly URL, parameter with slashes

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

Upvotes: 1

Views: 784

Answers (2)

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);

Upvotes: 0

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;
}

Upvotes: 2

Related Questions