Surendra Sharma

Surendra Sharma

Search This Blog

Saturday, September 12, 2015

How to point different URL of same website to single domain name in ASP.NET



If you are implementing Google analytics, Google treats http://www.example.com and http://example.com as different website and give different analytics reports. But in original both sites are same.

Now your client demanding that both URL http://www.example.com and http://example.com should redirect to single URL as http://www.example.com

How to solve this?

You must have URL Rewrite module in IIS for this. If you have not then download and install from http://www.iis.net/downloads/microsoft/url-rewrite

Now make entries in web.config between <system.webServer> … </system.webServer> tag by specifying old URL lists mapping with new single URL. Finally your config file entry looks like

<rewrite>
  <rules>
      <rule name="CanonicalHostNameRule1">
          <match url="(.*)" />
          <conditions>
              <add input="{HTTP_HOST}" pattern="^www\.example\.com$" negate="true" />
          </conditions>
          <action type="Redirect" url="http://www.example.com/{R:1}" />
      </rule>
  </rules>
</rewrite>  


You can do all these activities from IIS as well and IIS will make entry in web.config for you.


Please leave your comments or share this tip if it’s useful for you.

Thursday, September 10, 2015

One line of C# code to check item's base templates in Sitecore



As we know Sitecore allow both multiple and multi-level inheritance. If we template is inherited from multiple and multi-level inheritance then it’s very cumbersome to check the base template by using code. However here is simple scenario with only line of code to achieve it.

For example, suppose you have below content tree with parent node is Fruit

·         Fruits
o    Apple 1 : base template Apple
o    banana 1 : base template Banana
o    Apple 2 : base template Apple
o    Mango 1 : base template Mango
o    Mango 2 : base template Mango
o    Green Apple 1: base template Apple, Green Apple

Now if anyone want only items which are of Apple types then we can achieve it in single line of code as 

rptFruit.DataSource = ((Item)Fruits).Children.Where(x => x.Template.BaseTemplates.Any(y => y.ID.Equals(AppleTemplateId)));

If you are trying to access children of any item and want to access only those children items which are inherited from one particular type of template then use LINQ as <item>.Template.BaseTemplates.Any() method.
 
Please leave your comments or share this code if it’s useful for you.