I observed when I created a redirect and the source path ends with slash the redirect doesn't work.

Example:
source: test/this/
destination: node/10

Maybe could be fine validate it like the same way validates if there a slash at the beginning of the path.

Issue fork redirect-2932615

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

nachosalvador created an issue. See original summary.

nachosalvador’s picture

Assigned: nachosalvador » Unassigned
Status: Active » Needs review
StatusFileSize
new1.48 KB
nachosalvador’s picture

Issue summary: View changes
bkosborne’s picture

Title: Validate source path has slash at the end » Don't allow adding source paths with trailing slashes, they won't ever work
bkosborne’s picture

Status: Needs review » Needs work

Note that this is due to a change that was made in #2882881: Redirect with trailing slash is not working. Redirect treats /source and /source/ as two distinct redirects, but the change in that issue prevents /source/ from ever being used.

The patch could use a bit of work to update the wording and logic - will roll a new one this afternoon.

nachosalvador’s picture

bkosborne’s picture

Category: Feature request » Bug report
Status: Needs work » Needs review
StatusFileSize
new2.92 KB

Updated the language of the error message, changed the logic a bit, and updated all the validate functions to operate on a trimmed value of the redirect before testing their use cases. This is important because the value is trimmed before it's saved. We should only check the trimmed version.

Also changing this to a bug since without it, users can be very confused as to why redirects are not working.

Status: Needs review » Needs work

The last submitted patch, 7: 2932615-7.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

bkosborne’s picture

Hmm I think those are unrelated test failures.

droddis’s picture

Hey there,

I've read all the various issues and to be honest can't make sense of them. Is this patch the best way to make business-directory/example/ redirect to a new page on the site?

thanks in advance for the help!

jasonawant’s picture

Hi droddis,

No, this patch prevents the use of a trailing forward slash when adding a redirect.

I agree that Redirect should standardize how it stores redirects, and that is should enforce redirects w/out leading and trailing forward slashes.

This came up for our team b/c we implemented fast404 module that queries the redirect table directly before bootstrapping Drupal. See #2838359: Fast404 path checking is incompatible with Redirect module. Standardizing how the redirect is stored will improve compatibility with fas404 module.

Also, in previous fast404 work, see comment #7 #2743965: Fast 404 results in 404s on valid routes when using fast404_path_check, I found that Drupal core is removing leading and trailing slashes when generating path aliases, see PathautoGenerator::createEntityAlias() use of AliasCleaner::cleanAlias().

I'd argue that Redirect should normalize how it stores redirects...without leading and trailing slashes to be aligned with core's alias storage.

Setting to Needs Review to get a maintainer to review this. Looks the status update in #7 did not take.

jasonawant’s picture

If Redirect continues to allow adding source paths with trailing slashes, then we should reconsider the patch committed in #2882881: Redirect with trailing slash is not working, which uses $path = trim($path, '/'); to remove leading and trailing slashes in the RedirectRequestSubscriber::onKernelRequestCheckRedirect() before calling RedirectRepository::findMatchingRedirect().

As of right now, if you add a redirect with a trailing slash, redirect does not match the redirect with requested path that includes the redirect.

And, if you're using fast404 with this patch, you're getting fast404 response as expected, b/c it trims the path before querying the redirect table.

berdir’s picture

Status: Needs review » Needs work

I fully agree that we should normalize but I also think that we should simply fix user input when saving instead of making them do something that we can automate.

majdi’s picture

StatusFileSize
new2.93 KB

I could not apply Patch #7 using composer-patches, seems this related to git version, I applied the patch from git and recreate it.

majdi’s picture

StatusFileSize
new2.96 KB
new689 bytes

I had to add one more case for sources with a query string.

Sometimes you have a source with trailing slash and query string, for example, source/?query-string=5, This case was not covered by the original patch.

majdi’s picture

StatusFileSize
new1.59 KB
new3.42 KB

I add the trilling slash case with a query string case to the test.

henrikakselsen’s picture

Wouldn't a simple solution be to add a label "Do not use trailing slashes" under the source field?

bkosborne’s picture

Wouldn't a simple solution be to add a label "Do not use trailing slashes" under the source field?

I think a better UX is to not allow a configuration that is not valid. That's what form validations are for

berdir’s picture

I still prefer to just clean it instead of adding validation, there is no reason to require user interaction, if anything we could do both for an API-level validation. That's what #3032976: Remove trailing spaces from source url is apparently doing, lets combine these two issues, as this adds a test, we just need to adjust the expectation then.

bkosborne’s picture

Priority: Normal » Major

I think this is a major UX issue. We've had lots of users confused thinking redirect was broken because they add the source redirect with a trailing slash

bkosborne’s picture

Status: Needs work » Needs review
StatusFileSize
new2.62 KB

Here's an approach that Berdir wants - stripping the trailing slash for the user instead of throwing a validation error.

I'm removing the trailing slash in the validation method, but maybe it should be in an entity presave? If Berdir agrees I will change it to do that.

As I was writing this, I figured that most of this validation logic should probably happen in entity constraints so that programmatically created redirects can benefit from the same protections. But that's certainly out of scope for this issue.

No tests yet, but I can write them once I get approval on the direction from maintainer.

bkosborne’s picture

StatusFileSize
new942 bytes

Re-rolled to work with latest version.

edemidenko’s picture

Tested with 2932615-22.patch: and it works.
Also mentioned issue Redirect with trailing slash is not working is fixed and trims the path from both sides(l/r)

bkosborne’s picture

RE: #23, the issue you mentioned is already added a related issue here. That issue trims the trailing path from the request path but only when applying the path matching logic to the request. Redirect module still allows users to add a redirect with a trailing slash, even though that redirect will never work.

edemidenko’s picture

RE: #24, 'already added a related issue here' - I saw issue in related ones, just mentioned it (because it was discussed above) that issue is already fixed and it allows navigating to path with trailing slashes (ex. test//) and after inbound processing it will trim path to (ex. test) and redirect (if founds matching)...
After applying your patch and try to Edit URL redirect the path will 'rtrim' and save path without trailing slashes... so it works as intended along with fixed related issue

prempatel2447’s picture

StatusFileSize
new1.52 KB

Try this patch it will work for both trailing and without trailing slash path urls.

Status: Needs review » Needs work

The last submitted patch, 26: tralingslash-2932615-26.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

prempatel2447’s picture

prempatel2447’s picture

StatusFileSize
new1.95 KB

Try this patch it will work for both trailing and without trailing slash path urls.

kim.pepper’s picture

Issue tags: +Needs tests
kim.pepper’s picture

Issue tags: +#pnx-sprint
atloveday’s picture

I tried applying the patch and noticed when i import an csv that contains redirects with trailing slashes all the redirects are not working but redirects without trailing slashes in the csv file work as expected.

The expected behaviour is that redirects should work if the csv file contains redirects with or without trailing slashes.

Maybe this is an issue in the path_redirect_import module.

atloveday’s picture

I have tried the patch which does not work for me on first submit but if i edit the redirect and save it then works.

jhedstrom made their first commit to this issue’s fork.

jhedstrom’s picture

Status: Needs work » Needs review

I've updated this in an issue branch to include a test, and also return more or less to the approach in #22, but moved that logic to the pre-save method. Using pre-save will address the issue noted above with redirect imports, etc.

Anonymous’s picture

StatusFileSize
new1.8 KB
dwisnousky’s picture

StatusFileSize
new1.56 KB
dwisnousky’s picture

paulocs’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: -Needs tests

Merge request !12 is simple and efficient. Moving to RTBC.

atloveday’s picture

we have tested the patch 38 and it is working like a charm

  • dwisnousky authored 5118c6d on 8.x-1.x
    Issue #2932615 by Majdi, bkosborne, prempatel2447, jhedstrom,...
dave reid’s picture

Status: Reviewed & tested by the community » Fixed

Makes sense and thanks for the test coverage! Committed to 8.x-1.x.

Status: Fixed » Closed (fixed)

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