What is correct way for redirect non language code URLs to language code contained URL in .NET 6?

Viewed 46

My website's old URLs for products in (just) EN was:

{domain}/product/{permalink}

I want to redirect them to:

{domain}/en/product/{permalink}

According to other languages were:

{domain}/{de|fr|es}/product/{permalink}

What I tried to do is:

var rewriteOptions = new RewriteOptions()
    .AddRedirect("redirect-rule/(.*)", "redirected/$1/");

app.UseRewriter(rewriteOptions);

Old URLs to be remained:

{domain}/{de|fr|es|...}/product/{permalink}

Just one pattern to be changed:

{domain}/product/{permalink} to {domain}/en/product/{permalink}

What is the correct REGEX I have to use in redirect rules?

1 Answers

According to your description, if you want to just redirect the {domain}/product/{permalink} to {domain}/en/product/{permalink} and keep other url, I sugest you could try to use below redirect regex.

More details, you could refer to below codes:

        var rewriteOptions = new RewriteOptions()
.AddRedirect(@"^product/(.*)", "en/product/$1");

        app.UseRewriter(rewriteOptions);

Result:

enter image description here

Related