[英]Handle slash ('/') in a URL CodeIgniter
我正在使用CodeIgniter創建一個站點。 我有一個網址,例如http://mysite.com/albums/MJ/Dangerous.html
,其中MJ
是歌手的名字, Dangerous
是專輯的名字。 這兩個都是動態的,可以從數據庫中獲取。
數據庫中有一些專輯,其中帶有斜杠('/')字符。 因此,這樣的URL變為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顯示錯誤為“找不到頁面”。 沒有/
所有其他URL都可以正常工作。
嘗試使用雙URL編碼專輯名稱(即urlencode(urlencode($album))
)。 我試圖將URL一次傳遞給CodeIgniter控制器,但它總是給我帶來麻煩。 所以我只是對其進行了雙重編碼,然后在另一側突然彈出就沒問題了。 然后,我在傳遞的參數上運行urldecode()。
讓我知道這是否對您有幫助。
傳遞之前對值encode
$value = str_replace('=', '-', str_replace('/', '_', base64_encode($album)));
接收后decode
值
$value = base64_decode(str_replace('-', '=', str_replace('_', '/', $value)));
根據當前的SEO趨勢,無需在URL中添加引號或在頁面上獲取。 而是在保存(在礦渣類型的字段中)和檢索過程中,可以使用這種選項。
$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
擴展@MikeMurko答案以包括如何使用Javascript和PHP。 只需進行雙重編碼和解碼即可;
JS:
var id = encodeURIComponent( encodeURIComponent('mystring/&-34') );
PHP:
$id = urldecode( urldecode('mystring/&-34') );
您需要使用URL中字符的HTML轉義版本...
請參閱下面的參考。
http://www.theukwebdesigncompany.com/articles/entity-escape-characters.php
經過HTML編碼后,您的網址應類似於:
http://ringtones/albums/50-cent/Get Rich or Die Tryin'/The Massacre.html
我希望這有幫助!
ñ
從模型中檢索數據時,您將需要對專輯名稱進行urlencode編碼 ,以便轉義黑斜線。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.