I have 2 websites an old one called www.old.com and a new one called www.new.com . The old url has a 301 permanent redirect to the new website, but the new website has a different maproute so everything is not matching up the old website has the following type of route

      routes.MapRoute(
          "myroutes",
       "{controller}/{action}/{id}/{mytitle}", 
  new { controller = "buyer", action = "detail",mytitle=UrlParameter.Optional} 

        );

      old.com/buyer/detail/id/mytitle

the new website has the following

routes.MapRoute(
          "myroutes",              
          "{mytitle}/buyer/{id}",
          new
          {
              controller = "buyer",
              action = "detail",
              id = UrlParameter.Optional,
          });

new.com/mytitle/buyer/id

So when someone goes to the old url they get redirected to my new url but they encounter a 401 error as the Url become new.com/buyer/detail/id/mytitle which offcourse does not exist... How can i in my new URL reroute the old maproutes from the old website. I was thinking it could be done as a check in the controller/action of the new website or in the maproutes redirect it somehow anybody have any ideas?

有帮助吗?

解决方案

I wouldn't do this in a controller action as the request shouldn't really be reaching your MVC app with an incorrect route. It's unnecessary clutter when you can handle this with an IIS rewrite rule, by mapping capture groups:

<rule name="OldNewRule" stopProcessing="true">
  <match url="^([a-z]+)/([a-z]+)/([0-9]+)/([a-z0-9]+)$" ignoreCase="true" />
  <action type="Redirect" url="http://www.new.com/{R:4}/buyer/{R:3}" redirectType="Permanent" />
    <conditions>
        <add input="{HTTP_HOST}" pattern="www\.old\.com" />
    </conditions>
</rule>

Here, I am assuming that your ID is numeric and your title is alphanumeric. This will grab your capture groups {R:3} and {R:4} (which are 'id' and 'mytitle', respectively) and reformat them into the proper string, at the new domain.

Additionally, it requires that the original request was for the old.com domain, so this won't work if your existing rewrite happens first (you can probably remove the one you have entirely, or rework it to do what this is doing). This prevents it from redirecting any routes that match that were actually directed at the new domain, so you can still have MVC routes of this structure without them breaking (one clear advantage over doing this in a controller action).

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top