Welcome Guest, Not a member yet? Register   Sign In
trailing slash problem [Solved]
#1

[eluser]fchristant[/eluser]
hi,

I'm having somewhat of a complex URL problem. Let's start with the use case, this URL:

mydomain.com/user/19/gallery

...will display a list of images uploaded by user 19. The urls for the images on the page are calculated relative to the current URL (I'm not using things like "site_url"). Since there are tons of links on such a page, this keeps my HTML quite a bit shorter. A typical image URL on the page would be:

image/143

This is working fine, however, if I append a slash to the gallery page:

mydomain.com/user/19/gallery/

...things start to go wrong, the relative links now have the wrong URL:

user/image/143

...which breaks. Obviously I need to get rid of the "user/" part.

I am using URI routing, although I don't think that's the problem. Here's an example rule:

$route['image/(:any)'] = "image/view/$1";

My base URL is configured correctly, I'm sure of that (it has a trailing slash).

Now for the final part, the way I am calculating "how relative" the current URL is from the base_url:

Code:
$segments = $this->uri->total_segments();
$path = '';
switch ($segments) {
   case 1: $path = './'; break;
   case 2: $path = '../'; break;
   case 3: $path = '../../'; break;
   case 4: $path = '../../../'; break;
   default: $path = '';
}
$this->basepath = $path;

I know, I really should write that in a loop someday. Anyway, $this->basepath is what I prepend before all relative links on my views, like so:

Code:
$this->basepath . 'image/' . $row['image_id']

Here's what's going wrong: when there is a trailing slash in the URL, it is not counted as an extra segment, whilst in my scenario it should because of the calculation of the nesting. I tried to detect if the current URL has a trailing slash using current_url, but that doesn't work, it seems to return a URL of which the trailing slash is removed. Counting the URI segments does not work either, the trailing slash is not counted.

I'm hoping to solve this in a simple way in CI. In the URI manual I see there are special "r" functions for when one uses URI routing but I do not understand them, all they do is add a slash, even when there isn't one in the actual URL. Is there something I should do in the routing table? Or should I mess around with .htaccess files?

So many buttons to push :-)

Thanks in advance for any help!


Messages In This Thread
trailing slash problem [Solved] - by El Forum - 01-17-2009, 11:01 AM
trailing slash problem [Solved] - by El Forum - 01-18-2009, 04:47 AM
trailing slash problem [Solved] - by El Forum - 01-18-2009, 05:11 AM
trailing slash problem [Solved] - by El Forum - 01-18-2009, 06:17 AM
trailing slash problem [Solved] - by El Forum - 01-18-2009, 08:55 AM
trailing slash problem [Solved] - by El Forum - 01-18-2009, 11:20 PM
trailing slash problem [Solved] - by El Forum - 01-19-2009, 01:43 AM
trailing slash problem [Solved] - by El Forum - 01-19-2009, 01:50 AM
trailing slash problem [Solved] - by El Forum - 01-19-2009, 02:09 AM



Theme © iAndrew 2016 - Forum software by © MyBB