0

I want to redirect one URL to the another. Suppose, I have 2 URLs

  1. www.abc.com/home/gateway [Item for /gateway is not created in sitecore]
  2. www.xyz.com/newgateway.php [This is not a Sitecore Webpage]

So, here I want to redirect URL 1 to URL 2. In URL 1, /gateway item is not created but here I want if anybody hits that exact URL 1 it automatically redirected to URL 2.

I tried the IIS URL Rewrite module, it is not working correctly showing 404 Sitecore Error.

My Rule:

<rewrite>
    <rules>
        <rule name="My Rewrite" enabled="true" stopProcessing="true">
            <match url="/home/gateway" />
            <action type="Rewrite" url="www.xyz.com/newgateway.php" />
        </rule>
    </rules>
</rewrite>
  • @MarekMusielak I tried iisrewrite but is showing 404 error in Sitecore not redirecting. – PSS Mar 8 '19 at 15:12
  • 1
    Use <match url="home/gateway" /> without / at the beginning – Marek Musielak Mar 8 '19 at 15:22
  • @MarekMusielak I tried but same error. Showing 404 error page not redirecting to the URL 2. – PSS Mar 8 '19 at 15:32
  • Are you testing it in a fresh private browsing window each time? It's possible that your browser has cached the response. (You could also use Fiddler to make sure you're issuing a fresh request every time.) – Dan Sinclair Mar 8 '19 at 15:41
1

As an alternative you can write your own processor. For example like you would create a 404 processor, but then let it hook into a Sitecore list where you map url's to external links.

Like:

public class NotFoundRedirectProcessor: HttpRequestProcessor
{
    public override void Process(HttpRequestArgs args)
    {
        if (Sitecore.Context.Item != null || Sitecore.Context.Site == null)
        {
            return;
        }

        if (string.Equals(Sitecore.Context.Site.Properties["enableCustomErrors"], "true", StringComparison.OrdinalIgnoreCase) == false)
        {
            return;
        }

        var redirectUrl = GetRedirectUrl(args);
        if (redirectUrl  != null)
        {
          WebUtil.Redirect(redirectUrl, false);
          args.AbortPipeline();
        }

        var pageNotFound = this.Get404PageItem();
        args.ProcessorItem = pageNotFound;
        Sitecore.Context.Item = pageNotFound;
    }

    protected Item Get404PageItem()
    {
        // This is largely up to how the project in general is setup.
        // My solutions are heavily Fortis dependent, but for Vanilla 
        // Sitecore setups you could just use the following setting:
        // Settings.GetSetting("ItemNotFoundUrl", "/errors/404");
        // and pull the Item object from that path

        var path = Sitecore.Context.Site.RootPath + "/" + Settings.GetSetting("ItemNotFoundUrl", "/errors/404");
        var item = Sitecore.Context.Database.GetItem(path);

        return item;
    } 

    private string GetRedirectUrl(HttpRequestArgs args)
    {  
      //Add your code here to resolve from your Sitecore list the right url to redirect to
    }
}
-1

I would recommend using the Sitecore Url Rewrite module which allows you to configure IIS Url Rewrites in Sitecore items.

  • I tried to install the module you shared but it is throwing this error while installation - "The parent item of /sitecore/templates/User Defined/Url Rewrite/Redirect Folder/Site was not serialized. You cannot have a sparse serialized tree. You may need to serialize this item's parents." – PSS Mar 8 '19 at 15:27
  • I'm not familiar with that error, but did you try serializing the parents? – Dan Sinclair Mar 8 '19 at 15:30
  • No, I was just installing the package "URL Rewrite-1.8.2.zip", this error was thrown by sitecore installation wizard. – PSS Mar 8 '19 at 15:34
  • I would try serializing the parents, then, to see if that resolves the issue. – Dan Sinclair Mar 8 '19 at 15:35
  • It's also possible that that module does not support your version of Sitecore. It looks like it has been updated to work with 8.2, but may not have been tested with 9+. – Dan Sinclair Mar 8 '19 at 15:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.