Alright, I may just be not understanding the code, so I wanted to check with you. Here is the use case:
- We need trailing slashes added to links throughout the site.
- We don't want trailing slashes to be forced (read: redirected to) if the user goes to the page without the trailing slash.
- This is because we have custom paging that breaks if there is a trailing slash.
In theory, this should be just fine - and in fact it is right now. We have Trailing Slash installed, but no .htaccess code in place to force the rewrite, which means most system links are rewritten with the trailing slash, but the funky pager can write it's without the trailing slash and everything works.
Until we add Global Redirect. (Which unfortunately is needed for SEO reasons as this is a huge site migration).
When we add Global Redirect, suddenly the trailing slash is forced on everything - even when you go to the pager URL without the slash, it gets added. No problem, I thought, I'll just turn off the nifty "Enforce trailing slash" option and we'll be back to how things used to be. No dice.
I would think that the redirect to a URL with a trailing slash would no longer happen if that wasn't checked. Seems like it still is.
Without only Trailing Slash I can go to www.example.com/node/1 and it resolves. With Global Redirect it redirects to www.example.com/node/1/ regardless of the "Enforce trailing slash" setting.
That seems to make sense when I look at the code:
/**
* Implements hook_form_FORM_ID_alter().
*
* Change GlobalRedirect settings form to allow enforcement of trailing slashes.
*/
function trailing_slash_form_globalredirect_settings_alter(&$form, &$form_state, $form_id) {
if (variable_get('trailing_slash', TRUE)) {
$form['settings']['deslash'] = array_merge($form['settings']['deslash'], array(
'#title' => t('Enforce trailing slash'),
'#description' => t('If enabled, this option will enforce the trailing slash in requests. This stops requests such as example.com/node/1/ and example.com/node/1 causing duplicate content. On the other hand, if you require certain requests to not have a trailing slash, this feature can cause problems so may need to be disabled.'),
));
}
}
This alters the labels, but doesn't actually change anything unless Global Redirect somehow picks up that Trailing Slash is installed? Is that the case? I would have thought the trailing slash custom_url_rewrite_outbound function would need to pick up on if the Global Redirect deslash variable was set or not?
For the record, I've tried with both globalredirect-7.x-1.x-dev and globalredirect-7.x-1.4. Incidentally when I go to node/1/page/0/1/ in 7.x-1.4 it keeps that URL, but displays the node/1/ content. With 7.x-1.x-dev it actually switches the URL to node/1/.
| Comment | File | Size | Author |
|---|---|---|---|
| #5 | trailing_slash-fix_gr_no_enforce-1549700-5.patch | 3.39 KB | Akaoni |
Comments
Comment #1
sokrplare commentedHmmm...I'm stumped. I thought it through a bit more and really am wondering if the bug is actually that the trailing slash is only added when Global Redirect gets enabled. I suppose even the Smart Paging module paths should have trailing slashes added - so not really sure if this is an inconsistency in Trailing Slash, Smart Paging, or Global Redirect - or what combination thereof.
Comment #2
Akaoni commentedThanks for reporting this one.
I installed Smart Paging with Trailing Slash and Global Redirect and, other than a quirk with the pager dropdown, it seemed to work OK. Can you please provide me with more info on what doesn't work when it has a trailing slash?
You are right that turning the "Enforce trailing slash" option off in Global Redirect doesn't work - I hadn't actually tested this. Probably not an easy fix for this without changes to Global Redirect code. :(
Comment #3
sokrplare commentedHey Akaoni!
Odd, on a sandbox install I'm not seeing the Smart Paging issue either. This site implementation is a large one that others are working on too so perhaps they've done some overrides that are causing issues.
The Global Redirect setting is more the issue for our situation. Does the URL hit Global Redirect first or last? It seems to me like it hits Trailing Slash last (so it can be added there). If that is the case, could code in Trailing Slash resolve this? I don't follow the flow of a URL from start to end so am likely way off base here, just curious.
I suppose ultimately, if the Global Redirect "Enforce" setting isn't enabled, wouldn't this be the same thing as disabling trailing slash on the Clean URLs config page? Is there a difference?
(Thanks for your lightening quick reply by the way!)
Comment #4
Akaoni commentedI would recommend looking deeper into the issue Smart Paging has with Trailing Slashes, as supporting both trailing slashes and not isn't great for SEO:
http://googlewebmastercentral.blogspot.com.au/2010/04/to-slash-or-not-to...
Another thing you could do is switch to web server redirects for SEO (eg. mod_rewrite), rather than Global Redirect.
That said, I do need to fix the Global Redirect issue you raised so let's focus on that.
Global Redirect does these redirects on Drupal boot (before even processing the page). It doesn't provide any hooks for this process which is why the behavior is hard to alter without making code changes to Global Redirect itself.
I reckon I might know a way to fix the problem however - it won't be pretty coding wise, but should work.
Will hopefully post some code for you to test soon.
More Info about How Trailing Slash and Global Redirect Work
Trailing Slash and Global Redirect do two distinctly different things: Trailing Slash ensures that Drupal outputs links to URLs with trailing slashes (eg. <a href="/node/1/">Node 1</a>). Global Redirect determines when a user has arrived at an incorrect URL (in this case one without a slash) and redirects them to the right URL.
Trailing Slash off, Global Redirect enforce trailing slash off:
Default Drupal URL behaviour - no trailing slashes.
Trailing Slash on, Global Redirect enforce trailing slash off:
Every Drupal link a user clicks on will take them to a trailing slash URL. If a user does go to a non trailing slash URL they will not be redirected. This creates duplicate content for users and search engines.
Trailing Slash off, Global Redirect enforce trailing slash on:Edit: Not possible as turning Trailing Slash off makes Global Redirect enforce no trailing slash again.Every link a user clicks on will take them to a non trailing slash URL. They will then be redirected to the equivalent trailing slash URL. This effectively doubles the number of requests made to the web server and Drupal which is not good for web server performance.
Trailing Slash on, Global Redirect enforce trailing slash on:
Every Drupal link a user clicks on will take them to a trailing slash URL. If a user does go to a non trailing slash URL they will be redirected. This is the ideal.
Comment #5
Akaoni commentedOK, please try the following code/patch and let me know if it allows Global Redirect to not enforce trailing slashes.
Replace the whole trailing_slash_url_outbound_alter() function in trailing_slash.module file with:
Comment #6
sokrplare commentedTested and confirmed working! Brilliant!
Only question/concern - I think it is the only way, but is using debug_backtrace() "allowed" in a module? Know of any others that do it?
Amazing!
Comment #7
Akaoni commentedThanks for testing!!
Yeah, that's the "won't be pretty" part. :/
It's not ideal, but I'm willing to call this a fringe case.
Below is a tweak to help improve memory usage and release:
Committed and will include this in RC5.
Comment #8
Akaoni commentedTitle need make better english... o.O