简体   繁体   English

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

[英]Handle slash ('/') in a URL CodeIgniter

I am creating a site using CodeIgniter. 我正在使用CodeIgniter创建一个站点。 I have an url like http://mysite.com/albums/MJ/Dangerous.html where MJ is the artist name and Dangerous is the name of the album. 我有一个网址,例如http://mysite.com/albums/MJ/Dangerous.html ,其中MJ是歌手的名字, Dangerous是专辑的名字。 Both of these are dynamic and fetched from a database. 这两个都是动态的,可以从数据库中获取。

In the database there are some albums which have a slash ('/') character in them. 数据库中有一些专辑,其中带有斜杠('/')字符。 So, such an URL becomes http://mysite.com/albums/50-cent/Get+Rich+or+Die+Tryin%27%2FThe+Massacre.html . 因此,这样的URL变为http://mysite.com/albums/50-cent/Get+Rich+or+Die+Tryin%27%2FThe+Massacre.html On decoding, it turns out as http://ringtones/albums/50-cent/Get Rich or Die Tryin'/The Massacre.html . 关于解码,结果显示为http://ringtones/albums/50-cent/Get Rich or Die Tryin'/The Massacre.html Here the artist is 50-cent and the album name is Get Rich or Die Tryin'/The Massacre 这里的艺术家是50-cent ,专辑名称是Get Rich or Die Tryin'/The Massacre

My question is, what do I do so that I get the entire album name, ie Get Rich or Die Tryin'/The Massacre.html as a single argument with the slash character? 我的问题是,我该怎么做才能获得完整的专辑名称,即“ Get Rich or Die Tryin'/The Massacre.html作为带有斜杠字符的单个参数? Currently CodeIgniter shows an error as "Page not found" when I click on such an URL. 当前,当我单击这样的URL时,CodeIgniter显示错误为“找不到页面”。 All other URL's which doesn't have the / works fine. 没有/所有其他URL都可以正常工作。

Try double URLencoding the album name (ie urlencode(urlencode($album)) ). 尝试使用双URL编码专辑名称(即urlencode(urlencode($album)) )。 I was trying to pass a URL once to a CodeIgniter controller and it constantly gave me troubles. 我试图将URL一次传递给CodeIgniter控制器,但它总是给我带来麻烦。 So I just double encoded it, and then it popped through on the other side no problem. 所以我只是对其进行了双重编码,然后在另一侧突然弹出就没问题了。 I then ran an urldecode() on the passed parameter. 然后,我在传递的参数上运行urldecode()。

Let me know if that helps you. 让我知道这是否对您有帮助。

encode the value before passing 传递之前对值encode

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

decode the value after receiving 接收后decode

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

As per current SEO trend no need to bring the quote in url or to fetch on page. 根据当前的SEO趋势,无需在URL中添加引号或在页面上获取。 instead during saving (in slag kind of field) and retrieving you can use this kind of option. 而是在保存(在矿渣类型的字段中)和检索过程中,可以使用这种选项。

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

Result: 结果:

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

Extending the @MikeMurko answer to include how to work with Javascript and PHP. 扩展@MikeMurko答案以包括如何使用Javascript和PHP。 Simply go for double encoding and decoding; 只需进行双重编码和解码即可;

JS: JS:

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

PHP: PHP:

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

You need to use the HTML escaped version of the characters in your URL... 您需要使用URL中字符的HTML转义版本...

See below for a reference. 请参阅下面的参考。

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

After HTML encoding, your URL should look something like: 经过HTML编码后,您的网址应类似于:

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

I hope this helps! 我希望这有帮助!

N ñ

从模型中检索数据时,您将需要对专辑名称进行urlencode编码 ,以便转义黑斜线。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM