在URL CodeIgniter中处理斜杠(' /')

时间:2011-11-07 18:20:23

标签: php url-rewriting urlencode codeigniter-2 codeigniter-url

我正在使用CodeIgniter创建一个站点。我有一个类似http://mysite.com/albums/MJ/Dangerous.html的网址,其中MJ是艺术家姓名,Dangerous是相册的名称。这两个都是动态的,并从数据库中获取。

在数据库中有一些专辑中包含斜杠('/')字符。因此,这样的网址变为http://mysite.com/albums/50-cent/Get+Rich+or+Die+Tryin%27%2FThe+Massacre.html。解码时,结果为http://ringtones/albums/50-cent/Get Rich or Die Tryin'/The Massacre.html。此处艺术家为50-cent,相册名称为Get Rich or Die Tryin'/The Massacre

我的问题是,我该怎么办才能获得整个专辑名称,即Get Rich or Die Tryin'/The Massacre.html作为带斜杠字符的单个参数?当我点击这样的URL时,CodeIgniter当前显示错误为“找不到页面”。所有其他没有/的网址都可以正常使用。

6 个答案:

答案 0 :(得分:3)

尝试对相册名称进行双重URL编码(即urlencode(urlencode($album)))。我试图将一次URL传递给CodeIgniter控制器,它不断给我带来麻烦。所以我只是对它进行了双重编码,然后它突然出现在另一边没问题。然后我在传递的参数上运行了urldecode()。

如果有帮助,请告诉我。

答案 1 :(得分:1)

encode传递前的值

$value = str_replace('=', '-', str_replace('/', '_', base64_encode($album)));

decode接收后的值

$value = base64_decode(str_replace('-', '=', str_replace('_', '/', $value)));

答案 2 :(得分:0)

您需要在网址中使用HTML转义版本的字符...

请参阅下文以供参考。

http://www.theukwebdesigncompany.com/articles/entity-escape-characters.php

HTML编码后,您的网址应如下所示:

http://ringtones/albums/50-cent/Get Rich or Die Tryin'/The Massacre.html

我希望这有帮助!

<磷>氮

答案 3 :(得分:0)

从模型中检索数据时,您需要urlencode相册名称,以便转义黑名单。

答案 4 :(得分:0)

根据当前的SEO趋势,无需在网址中引用引号或在页面上获取。 而是在保存期间(在炉渣类型的字段中)和检索,您可以使用这种选项。

$base_url = "http://ringtones/";
$controller = "albums";

$artist = "50 cent";
$album = "Get Rich Or Die Tryin' / The Massacre";

$link1 = $base_url.$controller.'/'.url_title($artist,'-',TRUE).'/'.url_title($album,'-',TRUE);
echo $link1;

<强>结果:

http://ringtones/albums/50-cent/get-rich-or-die-tryin-the-massacre

答案 5 :(得分:0)

扩展@MikeMurko答案,以包括如何使用Javascript和PHP。 只需进行 double 编码和解码;

JS:

var id = encodeURIComponent( encodeURIComponent('mystring/&-34') );

PHP:

$id = urldecode( urldecode('mystring/&-34') );