Suman
Suman

Reputation: 49

Handle slash ('/') in a URL CodeIgniter

I am creating a site using 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. 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. On decoding, it turns out as 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

My question is, what do I do so that I get the entire album name, i.e. Get Rich or Die Tryin'/The Massacre.html as a single argument with the slash character? Currently CodeIgniter shows an error as "Page not found" when I click on such an URL. All other URL's which doesn't have the / works fine.

Upvotes: 2

Views: 3900

Answers (6)

Nick Cartwright
Nick Cartwright

Reputation: 8274

You need to use the HTML escaped version of the characters in your URL...

See below for a reference.

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

After HTML encoding, your URL should look something like:

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

Upvotes: 0

MR_AMDEV
MR_AMDEV

Reputation: 1942

Extending the @MikeMurko answer to include how to work with Javascript and PHP. Simply go for double encoding and decoding;

JS:

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

PHP:

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

Upvotes: 0

Rashmy
Rashmy

Reputation: 125

encode the value before passing

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

decode the value after receiving

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

Upvotes: 1

anish shah
anish shah

Reputation: 11

As per current SEO trend no need to bring the quote in url or to fetch on page. 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

Upvotes: 1

MikeMurko
MikeMurko

Reputation: 2233

Try double URLencoding the album name (i.e. urlencode(urlencode($album))). I was trying to pass a URL once to a CodeIgniter controller and it constantly gave me troubles. 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.

Let me know if that helps you.

Upvotes: 4

Yada
Yada

Reputation: 31265

You will want to urlencode the album name when the data is retrieved from the model so that blackslash is escaped.

Upvotes: 0

Related Questions