locked
how to redirect to new domain but keeping the rest url same ? RRS feed

  • Question

  • User-851246726 posted

    I have moved the site from www.domain.com to www.newsite.com

    Now I want to redirect from old domain to new domain but I want to keep the rest of the url / address same in order to maintain the integrity of the old URLs so that anyone clicking on a link to a page on your old website will be redirected to the same page on your new domain.

    Example: Clicking on www.domain.com/company/profile.asp.x would automatically redirect visitors to the same page on your new website www.newsite.com/company/profile.asp.x 

    FYI: I don't have access to IIS or anything as my old domain is hosted on a hosting server like Godaddy. Any way to acheive this by code or if I put a webconfig for old domain ?

    plez help

    Friday, October 18, 2013 11:04 AM

Answers

  • User-718146471 posted

    Notice that your code reads like this:

     

            <rule name="Canonical Host Name" stopProcessing="true">
              <match url="(.*)" />
              <conditions>
                <add input="{HTTP_HOST}" pattern="www\.OldDomain\.net$" negate="true" />
              </conditions>
              <action type="Redirect" url="http://www.NewSite.com/{R:1}" redirectType="Permanent" />
            </rule>
    

    Try rewriting it this way:

    <rule name="Canonical Host Name" stopProcessing="true">
      <match url="(.*)" />
      <conditions>
        <add input="{HTTP_HOST}" negate="true" pattern="^oldsite\.com$" />
      </conditions>
      <action type="Redirect" url="http://newsite.com/{R:1}" redirectType="Permanent" />
    </rule>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 21, 2013 11:51 AM

All replies

  • User-718146471 posted

    Do you have access to your DNS? If so, you can have the old domain be a mirror of the new. It will be the same site, just different DNS names.

    Friday, October 18, 2013 11:37 AM
  • User-851246726 posted

    I do have the access to the old DNS settings on web hosting control panel online. But what exactly I will be doing ?

    But I don't think mirroring with old domain to new domain will help as url will remain old I am assuming in the address bar ? The whole point of moving to new domain and navigating to new domain is to let people know that domain name has been changes. If I mirror the new domain with old, the old domain will remain same and people will never know about new domain.

    Is there any way that I can add a aspx page or web config file for old domain and if any request comes to old domain it navigates to new one. But the important part is that right now I do have a redirection setup for old domain by control panel but the problem is, if anyone is going to www.OldDomain.com/Contact, it navigates to my new domain homepage.

    But I just want to change the domain in redirection but keep the rest of the url.

    any way I can do this by code if I add dafault .aspx page or webconfig for old domain ?

    Friday, October 18, 2013 11:52 AM
  • User-718146471 posted

    Hm, well maybe url rewriting might help. In this case from what I understand of the article, you will only re-write the domain.com part to point to the newdomain.com.

    http://weblogs.asp.net/owscott/archive/2009/11/30/iis-url-rewrite-redirect-multiple-domain-names-to-one.aspx

    Let me know if you have more questions.

    Friday, October 18, 2013 12:01 PM
  • User-851246726 posted

    I don't have much experience with url rewriting so I would deeply appreciate if you could me with the code.

    My situation example: If anyone tries to go to www.OldDomain.com/company/profile.aspx, he/she should automatically redirect to the same page on your new website www.newsite.com/company/profile.aspx

    same rules for other urls too like www.OldDomain.com/company/contact to www.newsite.com/company/contact and so on...

    plz help me ?

    Friday, October 18, 2013 12:06 PM
  • User-718146471 posted

    This should do it:

     

    <?xml version="1.0" encoding="UTF-8"?> 
    <configuration> 
        <system.webServer> 
            <rewrite> 
                <rules> 
                    <rule name="CanonicalHostNameRule1"> 
                        <match url="(.*)" /> 
                        <conditions> 
                            <add input="{HTTP_HOST}" pattern="www\.olddomain\.com$" negate="true" /> 
                        </conditions> 
                        <action type="Redirect" url="http://www.newsite.com/{R:1}" /> 
                    </rule> 
                </rules> 
            </rewrite> 
        </system.webServer> 
    </configuration> 

    Friday, October 18, 2013 12:52 PM
  • User-851246726 posted

    I don't have to mention or add rule for each url ? just  this code in webconfig should do  that job ? 

    Should I add any default.aspx page or anything ? or I should just add this webconfig with this code in the root directory of old domain name ?

    Friday, October 18, 2013 1:31 PM
  • User-718146471 posted

    Exactly, it only should impact the domain part. Notice the {R:1} part, that should maintain the trailing part of the URL. Of course make sure you back up your web.config just in case. :) Better safe than sorry I say :)

    Friday, October 18, 2013 1:38 PM
  • User-851246726 posted

    thank you, I will test it and get back to you with results

    Friday, October 18, 2013 2:19 PM
  • User-851246726 posted

    Hello bbcompent1,

    I tested the code over the weekend on the server and here are  the results:

    1) If I type: OldDomain.com/Contact ->It works and navigate the user from old domain url to new domain url without any problem 

    2) If I type "www" in front of old domain like: www.OldDomain.com/Contact ->It does not work and does not navigate the user. In fact it gives following error.

    It need  to make it work for old domain in both cases if any user add www in front of old domain or not as many users do add it and many don't.

    Error:

    HTTP Error 404.0 - Not Found

    The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.

    <fieldset>

    Most likely causes:

    • The directory or file specified does not exist on the Web server.
    • The URL contains a typographical error.
    • A custom filter or module, such as URLScan, restricts access to the file.
    </fieldset>
    <fieldset>

    Things you can try:

    • Create the content on the Web server.
    • Review the browser URL.
    • Create a tracing rule to track failed requests for this HTTP status code and see which module is calling SetStatus. For more information about creating a tracing rule for failed requests,
    </fieldset>

    Monday, October 21, 2013 11:07 AM
  • User-718146471 posted

    Did I show you this? Take a look: http://ruslany.net/2009/04/10-url-rewriting-tips-and-tricks/ 

    3. Canonical Hostnames

    Very often you may have one IIS web site that uses several different host names. The most common example is when a site can be accessed via http://www.yoursitename.com and via http://yoursitename.com. Or, perhaps, you have recently changed you domain name from oldsitename.com to newsitename.com and you want your visitors to use new domain name when bookmarking links to your site. A very simple redirect rule will take care of that:

    1. <rule name="Canonical Host Name" stopProcessing="true">  
    2.   <match url="(.*)" />  
    3.   <conditions>  
    4.     <add input="{HTTP_HOST}" negate="true" pattern="^ruslany\.net$" />  
    5.   </conditions>  
    6.   <action type="Redirect" url="http://ruslany.net/{R:1}" redirectType="Permanent" />  
    7. </rule>  
    <rule name="Canonical Host Name" stopProcessing="true">
      <match url="(.*)" />
      <conditions>
        <add input="{HTTP_HOST}" negate="true" pattern="^ruslany\.net$" />
      </conditions>
      <action type="Redirect" url="http://ruslany.net/{R:1}" redirectType="Permanent" />
    </rule>

    To see an example of how that works try browsing to http://www.ruslany.net/2008/10/aspnet-postbacks-and-url-rewriting/. You will see in the browser’s address bar that “www” is removed from the domain name.

    Monday, October 21, 2013 11:17 AM
  • User-851246726 posted

    I have updated my code in webconfig and still facing the same problem..when url is WITHOUT "www",  then navigation work fine but when url INCLUDES "www" then it gives me error.

    Here is my code:

    <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
      <system.webServer>
        <rewrite>
          <rules>
            <rule name="Canonical Host Name" stopProcessing="true">
              <match url="(.*)" />
              <conditions>
                <add input="{HTTP_HOST}" pattern="www\.OldDomain\.net$" negate="true" />
              </conditions>
              <action type="Redirect" url="http://www.NewSite.com/{R:1}" redirectType="Permanent" />
            </rule>
           
          </rules>
        </rewrite>
      </system.webServer>
    </configuration>

    How to add a rule that takes care of everything even if any user type www ?

    Monday, October 21, 2013 11:45 AM
  • User-718146471 posted

    Notice that your code reads like this:

     

            <rule name="Canonical Host Name" stopProcessing="true">
              <match url="(.*)" />
              <conditions>
                <add input="{HTTP_HOST}" pattern="www\.OldDomain\.net$" negate="true" />
              </conditions>
              <action type="Redirect" url="http://www.NewSite.com/{R:1}" redirectType="Permanent" />
            </rule>
    

    Try rewriting it this way:

    <rule name="Canonical Host Name" stopProcessing="true">
      <match url="(.*)" />
      <conditions>
        <add input="{HTTP_HOST}" negate="true" pattern="^oldsite\.com$" />
      </conditions>
      <action type="Redirect" url="http://newsite.com/{R:1}" redirectType="Permanent" />
    </rule>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 21, 2013 11:51 AM
  • User-851246726 posted

    Great - it worked !

    It is working both ways not, with and without www

    Thanks for your  time and help !

    Monday, October 21, 2013 11:59 AM