Skip to content

Redirects to homepage adds an unnecessary trailing slash #355

@ymilin

Description

@ymilin

Description

Redirects to the Homepage are generated with a double slash.

When creating a BarrelStrength\Sprout\redirects\components\elements\RedirectElement, RedirectHelper::findUrl() sets newUrl to '/' on line 146.

https://github.com/barrelstrength/sprout/blob/0df50ee1ad01b1eba8c4abffb21579cbf95cf089/src/redirects/redirects/RedirectHelper.php#L144..L149

Then, when building the redirect URL, Call to RedirectElement::getAbsoluteNewUrl() will concatenate $baseUrl . $this->newUrl where $baseUrl will always already have a trailing slash.

$baseUrl is set with $baseUrl = Craft::getAlias($this->getSite()->getBaseUrl());

In Craft 5, implementation for Site::getBaseUrl() adds a trailing slash.
https://github.com/craftcms/cms/blob/0009105ffef21582c8474c7d0d96621e82374696/src/models/Site.php#L153..L172

We should either create RedirectElement with an empty newUrl, or check if newUrl === '/' when redirecting.

How to reproduce

Create a Redirect with:

  • Old URL: test-redirect
  • New URL: (leave empty)
  • Status Code: any
  • Match Strategy: Exact Match

Requests to /test-redirect should redirect to https://my-website.com/, but redirects to https://my-website.com//

Sprout Version

5.0.3

Craft Version

5.7.10

Database Type Version

MariaDB 10.5

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions