Skip to content

Commit

Permalink
Updated "NormalizeUrlParts" method to handle a few more edge cases
Browse files Browse the repository at this point in the history
  • Loading branch information
abjerner committed Feb 2, 2022
1 parent cd930d2 commit efe5d0f
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions src/Skybrud.Umbraco.Redirects/RedirectsUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
using Skybrud.Umbraco.Redirects.Models;

namespace Skybrud.Umbraco.Redirects {

internal class RedirectsUtils {

public static bool NormalizeUrlParts(RedirectItem redirect) {

string url = redirect.LinkUrl;
string query = null;
string query = redirect.LinkQuery;
string fragment = null;

// Isolate the fragment if specified in the URL
int pos1 = url.IndexOf('#');
if (pos1 >= 0) {
Expand All @@ -21,25 +21,28 @@ public static bool NormalizeUrlParts(RedirectItem redirect) {
// Isolate the query string if specified in the URL
int pos2 = url.IndexOf('?');
if (pos2 >= 0) {
query = url.Substring(pos2 + 1);
query += "&" + url.Substring(pos2 + 1);
url = url.Substring(0, pos2);
}

// Parse the "fragment" value
if (redirect.LinkFragment.HasValue()) {

string temp = redirect.LinkFragment;

// Isolate the fragment if specified in the "anchor" value (overwrites fragment from the URL)
var pos3 = redirect.LinkFragment.IndexOf('#');
var pos3 = temp.IndexOf('#');
if (pos3 >= 0) {
fragment = redirect.LinkFragment.Substring(pos3);
fragment = temp.Substring(pos3);
temp = pos3 > 0 ? temp.Substring(0, pos3 - 1) : string.Empty;
}

// Treat remaining anchor value as query string (append if URL also has query string)
if (redirect.LinkFragment.HasValue()) {
if (redirect.LinkFragment.IndexOf('?') == 0 || redirect.LinkFragment.IndexOf('&') == 0) {
query += "&" + redirect.LinkFragment.Substring(1);
if (temp.HasValue()) {
if (temp.IndexOf('?') == 0 || temp.IndexOf('&') == 0) {
query += "&" + temp.Substring(1);
} else {
query += "&" + redirect.LinkFragment;
query += "&" + temp;
}
}

Expand Down

0 comments on commit efe5d0f

Please sign in to comment.