My problem is very strange and I haven't found anyone else on here or in any searches on the subject to share this.

In "From" I enter http://website.org/[testpath] (the []'s simply indicate the form text box)

In "To" I enter [http://old.website.org/p/dia/action/public/?action_KEY=3776]

When I click "Save", the redirect url appears with the final slash stripped http://old.website.org/p/dia/action/public?action_KEY=3776

When I try to edit and add the slash back in, same result. Any one know why this might be happening? If I set the url as http://old.website.org/p/dia/action/public/%3Faction_KEY=3776, using the encoding %3F in place of the ?, everything works fine. While this is a "fix", I would prefer the slash would not be stripped out when using a ? immediately after it.

Comments

jaydub’s picture

Looks like this happens during the form validation in redirect_element_validate_redirect() in redirect.admin.inc which in turn calls _redirect_extract_url_options() which calls redirect_parse_url(). The net result being that the URL apart from the query string is set w/o the trailing slash. Any reason why you actually have to have a trailing slash there as that's not the normal form of a URL in this case.

ANDiTKO’s picture

Have you tried to import your redirects using the CSV import module?

universalhandle’s picture

I'm experiencing the same behavior. Commenting out line 1285 of redirect.module, i.e.,

  $url = trim($url, '/');

resolves the problem. However, having not worked with the Redirect module much in the past, I'm not sure what other repercussions this change would have. Does anyone have any thoughts on that?

In answer to Jaydub's question:

Any reason why you actually have to have a trailing slash there as that's not the normal form of a URL in this case.

... the answer is that /p/dia/action/public/?action_KEY=3776 is different from /p/dia/action/public?action_KEY=3776. In some PHP applications, the first URL would pass the parameters in the query string to a file located at /p/dia/action/public/index.php, while the second URL would pass the parameters to a file located at /p/dia/action/public (i.e., a file named "public" with no file extension).

It seems to me that:

  • the redirect module should respect the destination URL as entered by the user, and
  • the user should not be required to encode the URL in order for the module to do so.

What would break if we stopped trimming the trailing slash?

capellic’s picture

I am having the exact same problem as @GinkgoFJG -- it's a DemocracyInAction.org URL that's having it's trailing slash stripped out. Considering that we like to have a short URL on our site to then redirect to the donation for marketing reasons, this has a significant impact on the value of our site.

I have made the change recommended in #3 and tested all my existing redirects. All look good, no bad side effects from what I can tell.

mfb’s picture

Version: 7.x-1.0-beta4 » 7.x-1.x-dev
Component: User interface » Code
Status: Active » Needs review
StatusFileSize
new300 bytes

Here's the patch.

The line of code in question has no comments so it's unclear what side effects there might be. However it appears to only be relevant for external URLs - slashes on internal drupal paths are still trimmed with the patch.

rgchi’s picture

Thank you for the patch. This worked!

Maury Markowitz’s picture

I am also stymied by this problem... I'm redirecting Drupal pages into a Presta ecommerce shop, which uses trailing /s extensively.

How do I apply this patch?

leewillis77’s picture

StatusFileSize
new547 bytes

I hit this problem as well, but the patch supplied wasn't sufficient for us. The patch attached works for us.

leewillis77’s picture

https://drupal.org/node/1812696 closed as a duplicate of this issue.

Status: Needs review » Needs work

The last submitted patch, redirect-1551224-trailing-slash-8.patch, failed testing.

leewillis77’s picture

Status: Needs work » Needs review
StatusFileSize
new567 bytes

Last patch was based off the wrong branch - oops. Revised patch attached.

torotil’s picture

Status: Needs review » Reviewed & tested by the community

Patch looks good and it passes the tests. Also it doesn't touch the handling of relative vs. absolute paths. Marking this as RTBC.

pere orga’s picture

Assigned: k.elizabeth » Unassigned
Issue summary: View changes
dave reid’s picture

Status: Reviewed & tested by the community » Needs work

I would think we will actually want to leave this behavior for "internal" Drupal paths, and only leave trailing slashes for external URLs.

mfb’s picture

Status: Needs work » Needs review

Patch at #5 still applies and works as you describe

dave reid’s picture

Status: Needs review » Needs work

No it does not. It should still trim the trailing slash for local paths. The patch in #5 trims trailing slashes for all paths.

mfb’s picture

The patch in #5 trims the trailing slash for all paths unless the URL has a query or a fragment. In this case, the trailing slash is preserved.

I think that's ok because 1) It seems unlikely that a user would accidentally type in a trailing slash to a Drupal path with a query or a fragment; and if they did it would only need to be removed for cosmetic reasons. And 2) It seems good to have similar behavior for all URLs, because redirect module (as far as I can tell) doesn't differentiate between "internal" Drupal paths and other local paths. For example, if you have static content or another web app sharing a base URL with a Drupal site, you may need both external and local URLs to contain /?

So I think #5 is good to commit, but I attached a patch that works as you describe.

Note, some URLs without a query or fragment may also need to preserve a trailing slash. I think this may be what #8 alludes to. That's not what this issue is about, although we could change the title.

mfb’s picture

Status: Needs work » Needs review
sonicthoughts’s picture

can the trailing slash be a configuration option - would be ideal to let this be explicit since these are nasty things to track down :)

acy76’s picture

StatusFileSize
new1008 bytes

The patch in #18 failed to work for me due to a prior call to trim() in the redirect_parse_url() function stripping out trailing slashes. Attached is a refactoring of the previous patch that fixes the issue. Please review.

mfb’s picture

for the patch in #23 I'd suggest changing the title of this issue to "External redirect URL that ends with / or includes /? strips / upon save"

acy76’s picture

@mfb I would agree that a new title might make sense here, since the basic issue is the same whether we're dealing with query parameters or not (i.e., the trailing slash is being stripped in all cases from the path component). How about "Trailing slashes unconditionally stripped from redirect URLs" or something along those lines? Seems to cover both cases.

kvnm’s picture

#18 worked great for me

kvnm’s picture

Status: Needs review » Reviewed & tested by the community
pifagor’s picture

RTBC

alex_optim’s picture

#18 Good for me too.

pifagor’s picture

  • pifagor committed 6ade553 on 7.x-1.x authored by acy76
    Issue #1551224 by mfb, leewillis77, acy76, Dave Reid, pifagor, kvnm,...

  • pifagor committed 9d150e7 on 7.x-2.x
    Issue #1551224 by mfb, leewillis77, acy76, Dave Reid, pifagor, kvnm,...
pifagor’s picture

pifagor’s picture

Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.