Cleaning up URL variables

Heres a handy function for processing URLs – I generally use it (and a PHP version) for cases where I run a lot through a central mod_rewrite controller:

sub real_url_path {
	my $url = shift;
	my @url_parts = split '/', $url;
	my @real_path_parts;
	for my $part (@url_parts) {
		print "part is: $part \n";
		if($part eq '.') {
			next;
		} elsif($part eq '..' || $part eq '...') {
			pop @real_path_parts;
		} elsif($part ne '') {
			push @real_path_parts, $part;
		}
	}
	my $real_path = '/' . join '/', @real_path_parts;
	if(substr($url,-1,1) eq '/' && length($real_path) > 2 ) {
		$real_path .= '/';
	}
	return $real_path;
}

This sub will remove all references to “../” corrently (/path/../foo/bar becomes /foo/bar).

  1. No comments yet.

  1. No trackbacks yet.