locked
Single Page 301 Redirect RRS feed

  • Question

  • User1567530616 posted

    I need to do a 301 redirect for a single page in my ASP.Net Core 3.1 web app.

    The page is being directed from this website to another one (which is a subdomain of the same top level domain).

    I have no idea how to do that and would appreciate some specific guidance on the how to accomplish the single page 301 redirect.

    Friday, October 23, 2020 9:03 PM

Answers

  • User753101303 posted

    Ah and this code is before UseStaticFiles() ?

    This method is to configure your http processing pipeline and the ordering does matter: you want to process redirect rules before trying to serve an existing file.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 6, 2020 8:39 AM

All replies

  • User-821857111 posted

    You can use the RedirectToPagePermanent method to redirect to the new location. 

    public void OnGet()
    {
        return RedirectToPagePermanent("/new-page");
    }



    Friday, October 23, 2020 9:16 PM
  • User1567530616 posted

    I should have mentioned that the page I am trying to 301 redirect is for a podcast so it is an xml page. 

    What is the best way to redirect that page?

    Friday, October 23, 2020 9:59 PM
  • User-821857111 posted
    public IActionResult OnGet()
    {
        return RedirectPermanent("url");
    }

    https://www.learnrazorpages.com/razor-pages/action-results

    Saturday, October 24, 2020 5:48 AM
  • User1567530616 posted

    Thank you so much for your answer. I'm sure it makes sense to many people, but I'm a little new at this. I have no idea of how and where to implement the code.

    And the page that I need to redirect is a podcast xml file. Does what you are suggesting still apply?

    I appreciate you help.

    Saturday, October 24, 2020 8:05 PM
  • User-821857111 posted

    The solution will redirect to any location specified in the parameter to the RedirectPermanent method.

    I have no idea of how and where to implement the code.

    The example I provided is in a Razor Page. You might need a different solution based on your requirement. Can you provide more detail about the redirection you want to effect? Perhaps the actual URL that the request is coming in on, and the URL that you want to redirect to (with the domain part obscured, if you prefer).

    Sunday, October 25, 2020 8:17 AM
  • User1567530616 posted

    We have a podcast and currently the podcast feed file (xml) is located on our Azure website here:

    http://www.xxxxxxxxxx.com/podcast/podcast.xml

    We are moving our podcast feed to a new location (which happens to be mapped to one of our Azure storage locations) here:

    http://slpodcast.xxxxxxxxxx.com/podcast/podcast.xml

    In order to have current subscribers moved from our old podcast feed to our new podcast feed automatically, I apparently need to do a 301 redirect from the old feed to the new feed. 

    I don't know the proper way to do this in my Azure ASP.NET Core 3.1 web app and was hoping that someone could give me some clear direction on how to do that.

    Thanks for your help.

    Sunday, October 25, 2020 2:28 PM
  • User-474980206 posted

    The original azure site needs to do the redirect. Where you using the static file handler? Anyway in the azure site you need to map all xml requests to a controller that redirect to the new location.

    Sunday, October 25, 2020 4:12 PM
  • User1567530616 posted

    Thanks. I'm looking for some direction on how to very specifically do that. (I am somewhat of a novice.)

    Sunday, October 25, 2020 4:45 PM
  • User1312693872 posted

    Hi, searchlightadmin

    If you want to use 301 redirect in azure asp.net core 3.1, you can view the following link:

    https://stackoverflow.com/questions/52021944/301-redirect-in-azure-asp-net-core-website

    Or you can try to use CNAME to make the redirect work, a CNAME record maps a specific domain, such as contoso.com or www.contoso.com, to a canonical domain name, more details are in this link, you can refer to it:

    https://docs.microsoft.com/en-us/azure/cloud-services/cloud-services-custom-domain-name-portal

    Best Regards,

    Jerry Cai

    Tuesday, October 27, 2020 6:17 AM
  • User1567530616 posted

    I'm still looking for an answer to this if anyone has a suggestion.

    Thanks!

    Tuesday, October 27, 2020 5:43 PM
  • User1312693872 posted

    Hi, searchlightadimin

    As long as the old url 'http://www.xxxxxxxxxx.com/podcast/podcast.xml' still exists and can be find, then you can get the xml file.

    they are right, you can redirect to the new url:

    public class IndexModel : PageModel
        {
           ....
            public IActionResult OnGet()
            {
                return RedirectPermanent("https://github.com/Leonidas-from-XIV/node-xml2js/blob/master/lib/builder.js");
            }
    ....
        }

    Best Regards,

    Jerry Cai

    Thursday, October 29, 2020 7:49 AM
  • User753101303 posted

    Hi,

    Or the problem is that this XML file is not currently served using code and so you don't know what to do with the provided code? An alternate option could be to use  https://docs.microsoft.com/en-us/aspnet/core/fundamentals/url-rewriting?view=aspnetcore-3.1 and AddRedirect.

    In short you configure the application at startup to catch the incoming url and tell the browser to fetch some other location instead.

    Thursday, October 29, 2020 9:42 AM
  • User1567530616 posted

    Yes! Thank you for clarifying the issue. The xml is really an externally created file that resides in a folder on the website. (That is one reason that I am moving it out of the website.)

    I tried AddRedirect as you suggested (per the linked article). It did not do the redirect so I must be doing something wrong even though it seems straight forward.

    I added this code:

    {
      var options = new RewriteOptions()
        .AddRedirect("www.xxxxx.com/podcast/podcast.xml", "http://slpodcast.xxxxx.com/podcast/podcast.xml", 301);
       app.UseRewriter(options);
    }

    That code was added to:

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env) {

    Friday, October 30, 2020 4:04 AM
  • User1312693872 posted

    Hi,searchlightadmin

    The issue is from the first parameter of the AddRedirect, you should use regex to match the url.

    You can follow this link to write regex depends on your needs.

    Best Regards,

    Jerry Cai

    Monday, November 2, 2020 8:38 AM
  • User1567530616 posted

    Thanks. I have played around with this and don't really understand how this works. Perhaps I'm just not getting the right syntax.

    Can someone show me how the first parameter should look?

    Wednesday, November 4, 2020 3:42 PM
  • User753101303 posted

    I tried and it gave me the working code below:

    var options = new RewriteOptions().AddRedirect(@"^podcast/podcast\.xml$", "https://forums.asp.net/");
    app.UseRewriter(options);

    See https://docs.microsoft.com/en-us/dotnet/standard/base-types/regular-expression-language-quick-reference 

    ^ and $ is the string start and end so I'm matching exactly that and not a file that would be at archive/podcast/podcast.xmll or that would have an appended query string value. (after a test I see the tested value does NOT include the query string as well, so in short this is really the virtual path relative to the web app root)

    . is any character and needs to be escaped

    I also used ^ and $ to check if a leading / is included (so no). Also it doesn't include the protocol or host name (you have specialized method to handle that). A side effect is that you can use the same rule without having to take into account if you are on localhost or your production site.

    Edit: also a benefit of using a regular expression is that you can "capture" and then reuse a part of the pattern. For example if you have 200 XML files in a folder, you can still redirect them all somewhere else using just a single rule.

    Edit : also it is case sensitive. You can use instead (?i)^podcast/podcast\.xml$ ie the same prefixed with (?i) to make this expression case insensitive.

    Wednesday, November 4, 2020 4:46 PM
  • User1567530616 posted

    Hmmm.

    I've tried what exactly what you suggested . . . and many variations.

    The key thing is the subdomain. The rest of the URL is the same, but I currently have my podcast file with my website at www

    I am moving it to a new subdomain which is not with my website. So . . .

    Old Location: www.domain.com/podcast/podcast.xml

    New Location: slpodcast.domain.com/podcast/podcast.xml

    Nothing I tried so far in the .AddRedirect seems to be picking up the first paramter (the Old Location).

    Any help would be appreciated!

    Thursday, November 5, 2020 8:57 PM
  • User753101303 posted

    The root for your current web application is www.domain.com or www.domain.com/podcast ? What if you start with "podcast\.xml" ie any path that contains podcast.xml ?

    Of course this rule is in the code of the site that is serving the old podcast.xml file or do you try to put this rule on your new site ???

    Thursday, November 5, 2020 10:57 PM
  • User-474980206 posted

    Hmmm.

    I've tried what exactly what you suggested . . . and many variations.

    The key thing is the subdomain. The rest of the URL is the same, but I currently have my podcast file with my website at www

    I am moving it to a new subdomain which is not with my website. So . . .

    Old Location: www.domain.com/podcast/podcast.xml

    New Location: slpodcast.domain.com/podcast/podcast.xml

    Nothing I tried so far in the .AddRedirect seems to be picking up the first paramter (the Old Location).

    Any help would be appreciated!

    in your example, the old site needs the redirect rule to the new site. so to have the redirect, you need to keep the old site running and have it redirect rather than return content.

    what type of site was the old site?

    Thursday, November 5, 2020 11:43 PM
  • User1567530616 posted

    The root of the application is www.domain.com

    I tried your suggestion and started with "podcast\.xml" I tried just "xml" because this is the only xml file on my website.

    I'm wondering whether my code is even being executed.

    Here is how the code looks now:

                {
                    var options = new RewriteOptions()
                        .AddRedirect(@"podcast\.xml", "https://google.com", 302);                
                    app.UseRewriter(options);                
                }

    (Note: I'm temporarily just trying to have it redirect to google.com to eliminate any issue on that parameter.)

    This code is in startup.cs under:

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)

    Thursday, November 5, 2020 11:46 PM
  • User1312693872 posted

    Hi,searchlightadmin

    I tested the code but it works well. I will tell you how I tested:

    First I copied you code and input 'podcast/podcast.xml' just behind my 'https://localhost:44320/', like:

    (https://localhost:44320/podcast/podcast.xml), then the url will redirect to google.

    I tried just "xml" because this is the only xml file on my website.

    Can you show me what you input in the url?

    Best Regards,

    Jerry Cai

    Friday, November 6, 2020 8:18 AM
  • User753101303 posted

    Ah and this code is before UseStaticFiles() ?

    This method is to configure your http processing pipeline and the ordering does matter: you want to process redirect rules before trying to serve an existing file.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 6, 2020 8:39 AM
  • User1567530616 posted

    Yes!

    That was the issue. I needed UseStaticFiles() after my redirect code, but I had it before.

    It now work.

    Thanks!

    Friday, November 6, 2020 8:51 PM