//------< redirect old folders and
keywords in URL >------
//*loop through all website, images and
files and check if it contains old url. redirect them permanent and break
process
//*old url paths by title
//*like
https://localhost:44396/Net-Framework/Files-Folders
app.Use(async (context, next) =>
{
//--------<
@Loop: html, images and files >--------
string sURL_Path =
context.Request.Path.Value;
if (sURL_Path.Contains(".")) //*check for IsFile
{
//if file
like *.js or *.css do nothing
}
else if (Regex.IsMatch(sURL_Path, @"=|๐ |๐|๐|๐|๐|๐", RegexOptions.IgnoreCase)) //*if /๐/๐=
{
//if
redirect with parameters
}
//*default
pattern: "๐/{IDArticle}",
else if (context.Request.QueryString.HasValue)
{
//----<
Check for numeric ?123 >----
string sQuerystring =
context.Request.QueryString.Value; //?1234
string sNumericTest =
sQuerystring.Substring(1);//1234
if (float.TryParse(sNumericTest, out _))
{
//-< isNumeric >-
string sNewURL = "/๐/"
+ sNumericTest;
//context.Request.Path = sNewURL; //*simple rewrite does not work
context.Response.Redirect(sNewURL,
permanent: true); //guide to new url
return;
// short
circuit
//-</
isNumeric >-
}
//----</
Check for numeric ?123 >----
}
else if (context.Request.QueryString.HasValue
== false) //*check for old URL-Folder Paths
{
//----<
QueryParameters is empty not ?123 >----
string sPath = context.Request.Path;
if (sPath.StartsWith("/")) sPath = sPath.Substring(1);
if (sPath != "")
{
//-< change path to parameters >-
string sNewURL = "";
if (sPath.Contains("Search/",
StringComparison.CurrentCultureIgnoreCase))
{
string sSearchWords = sPath.Replace("Search/", "",
StringComparison.InvariantCultureIgnoreCase);
sSearchWords =
Correct_old.decode_SemiCharactoers_in_Path(sSearchWords);
//*old
folders in path
sNewURL = "/๐?๐="
+ sSearchWords;
}
else
{
//*old
folders in path
string sNewFolderPath =
Correct_old.decode_SemiCharactoers_in_Path(sPath);
sNewURL = "/๐?๐="
+ sNewFolderPath;
}
//option1: rewrite without changing
visible URL
//context.Request.Path = sNewURL;
//note: .redirect dos not solve ~/
context.Response.Redirect(sNewURL,
permanent: true);
return;
// short
circuit
//-</ change path to parameters >-
}
//----</
QueryParameters is empty not ?123 >----
}
// next !
Important
await next();
//--------</
@Loop: html, images and files >--------
});
//------<( redirect old folders and
keywords in URL >------
|