This question came up because if one removes the trailing slash in Google Analytics content drilldown it is listed separate from sub paths. It seems like the more natural behavior from a GA perspective is to enforce having a trailing slash.

Thoughts?

Comments

entrigan’s picture

Title: Why not enforce having a trailing slash? » Enforce Trailing Slash Option
Category: support » feature

I notice that a lot of sites enforce trailing slashes. I think this also plays better with google analytics. Is there a reason not to do this?

donquixote’s picture

All link generation in drupal will produce links without the trailing slash. It's not trivial to change that.
Better have no trailing slash than having a redirect on every request.

entrigan’s picture

hmm, good point. Could not that be solved with a custom_url_rewrite_outbound() function? Either way I am not sure the benefit is outweighed by the cost.

donquixote’s picture

I am sure you would see a lot of places where this would have no effect, because the module author did not care to use url().

donquixote’s picture

Btw, you can find a similar discussion for the django framework somewhere, where someone (myself) asked to have an option to have trailing slashes removed. feature was rejected, last time i looked.

i think it depends very much on the framework/platform/cms you use. the authors/maintainers sometimes have a very strong opinion on stuff like that, and think their way is the best. and then there is no option to change it. which makes life a bit easier, if you simply accept it.

Akaoni’s picture

I've written code that alters outbound URLs with a trailing slash in D7:
http://drupal.org/sandbox/Akaoni/1145254

Oddly enough, when outbound URLs have trailing slashes, the Global Redirect deslash feature actually enforces them.

Is this code something that could be included in Global Redirect or is URL altering outside it's scope?
It'd need to be back ported to and tested in D6, but with the custom_url_rewrite_outbound() function this'd be easy enough.

Bilalx’s picture

I would be interested in drupal 6 version. Right now, i am hacking core and path auto to enforce the use of a trailing slash. And i just realized that i need to hack views too.

Akaoni’s picture

@Bilalx: Core and module hacks? Yikes!!
You can't just use custom_url_rewrite_outbound()?

Something like:

function custom_url_rewrite_outbound(&$path, $options, $original_path) {
  $path = preg_replace('/((?:^|\\/)[^\\/\\.]+?)$/isD', '$1/', $path);
}
Bilalx’s picture

Thanks, it works for arguments in views but not for the base views path.

I still had to hack path.inc to remove the trim from:

$_GET['q'] = drupal_get_normal_path(trim($_GET['q'], '/'));

Edit:

Adding:

function custom_url_rewrite_inbound(&$result, $path, $path_language) {
  $result = rtrim($result, '/'); 
}

seems to have taken care of the trailing slash when a view has no arguments.

Akaoni’s picture

Glad to help.

Something like this may help for views with and without arguments:

function custom_url_rewrite_inbound(&$result, $path, $path_language) {
  $result = preg_replace('/(^.*?)\\/?(#|\\?|$)/isD', '$1$2', $result);
}
Bilalx’s picture

Thanks, your code seems to be more comprehensive than mine :)

Akaoni’s picture

Welcome. ;)

Since no one related to Global Redirect seems interested, I'll probably release Trailing Slash as it's own project and Global Redirect can then be used to enforce slashes.
This code should come in handy to roll into a D6 version.