How to move a URL for a ASP.NET MVC Site - asp.net-mvc

My SEO colleague has fired in a few url requests that involve moving
www.mydomain.co.uk/news
www.mydomain.co.uk/news/{id}/{title}
to
www.mydomain.co.uk/uk-news
www.mydomain.co.uk/uk-news/{id}/{title}
What is the best way to carry this out.
I was just thinking of changing the route in the global.asax file
FROM:
routes.MapRoute(
"News", // Route name
"News", // URL with parameters
new { controller = "News", action = "Index" } // Parameter defaults
);
TO:
routes.MapRoute(
"News", // Route name
"uk-news", // URL with parameters
new { controller = "News", action = "Index" } // Parameter defaults
);
and setting up a 301 redirect on the basic www.mydomain.co.uk/news
Is there a better way to do this?

I don't think so. Your on the right track, changing the URL on the route and doing the 301 permanent redirect is the way to go. For the 301 try using the URL Rewrite Module.
Rewrite Module
<!-- Web.config file in subdirectory to be redirected -->
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="RedirectRule" stopProcessing="true">
<match url="(.*)" ignoreCase="true" />
<action type="Redirect" url="http://www.newdomain.com/{R:1}" redirectType="Permanent" />
<conditions>
<add input="{HTTP_HOST}" pattern="www\.old-domain\.com" />
</conditions>
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
ISAPI_Rewrite
RewriteCond Host: ^mydomain\.co.uk
RewriteRule /news http://www.mydomain.co.uk/uk-news [I,O,RP,L]
I (ignore case)
Indicates that characters are matched regardless of a case. This flag affects RewriteHeader directive and all corresponding RewriteCond directives.
O (nOrmalize)
Normalizes string before processing. Normalization includes removing of an URL-encoding, illegal characters, etc. Also, IIS normalization of an URI completely removes query string. So, normalization should not be used if query string is needed. This flag is useful with URLs and URL-encoded headers.
RP (permanent redirect)
Almost the same as the [R] flag but issues 301 (moved permanently) HTTP status code instead of 302 (moved temporary).
L (last rule)
Stop the rewriting process here and don't apply any more rewriting rules.

Related

How can I force all IIS requests to go to one MVC controller & action?

I have a situation that I figured would be quite easy to do in MVC.
I want to have all request on a IIS site end up going to one specific action.
So I added the following to my route
routes.MapRoute(
"Everything",
"{*foo}",
new { controller = "Home", action = "Index" }
);
That works for most request going to the server, but there are some things the are ending being dealt with by other handlers:
http://foo.com/ (works)
http://foo.com/bar (works)
http://foo.com/bar/baz (works)
http://foo.com/bar/baz.txt (works - used to not work, but started working after adding runAllManagedModulesForAllRequests="true" in web.config)
http://foo.com/bar/baz. (does not work. stumped as to why)
This is in MVC 5, but I don't think that should really make a difference to the solution.
What trick am I missing here?
I would use the url rewrite module for this. Something like this should do it. (Obviously, change the url to whatever you want):
<rewrite>
<rules>
<rule name="Catch-All" stopProcessing="true">
<match url=".*" />
<action type="Rewrite" url="/index.html" appendQueryString="false" />
</rule>
</rules>
</rewrite>

Force a language prefix in the url with Sitecore

Is there a setting to force the language in the URL? Like, if I browse to http://www.site.com, I should be redirected to http://www.site.com/en, as it is now I can see the start page without the language prefix.
The LinkManager is configured to always put in the prefix so all the links look fine at least.
Another way to use is our SEO Friendly URL Module.
This module implements a custom LinkProvider that provides SEO Friendly URL's and forces items to be accessed through their friendly URL.
So if an item is accessed without the language code in the URL (e.g. /my-item) the module will 301 redirect to the URL with language code (e.g. /en/my-item).
That is, if you have configured it to force friendly URL's (forceFriendlyUrl="true") and set languageEmbedding="always".
We use that module on our corporate website, so take a look at that to see it in action.
You can use ISS URL Rewrite to redirect / to /en
Install that module on the IIS, and setup a rule.
It can be done from the IIS gui or from web.config
I am using this rule to do the same.
Insert the following configuration in the system.webServer section in web.config
<system.webServer>
<rewrite>
<rule name="redirert / to en" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAny">
<add input="{PATH_INFO}" pattern="^/$" />
</conditions>
<action type="Redirect" url="/en/" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
It will redirect every request hitten "/" to "/en"

MVC routing for both .mvc and extensionless URLs

I have an odd question. I'm working on some MVC code that was setup to run extensionless, as in /Home/Index?id=10 However, it used to be setup for IIS 6, and was using the .mvc extension on all the controllers, as in /Home.mvc/Index?id=10.
I would like both routes to be able to work. In my global.asax.cs file, I created the following code:
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapRoute("Default2",
"{controller}.mvc/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute("Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
This almost worked! But unfortunately when my controllers return RedirectToAction, the URL which is generated fails.
I call RedirectToAction like this:
return RedirectToAction("Index", "Account");
and it returns this:
/Account.mvc
Now Index is assumed so I'm not worried that that's missing. However, there is no / on the end of that URL, and because of that, it is returning a 404 error. If I add the / it works fine. Is there some way I can help MVC to generate the URLs correctly, or is there a way I can modify the routes to make it recognize this URL?
Background: I'm in this situation because I began converting my application to use extensionless URLs after we upgraded our webserver to IIS 7.5. I didn't realize that there were alot of links into the applications which had been shared with the outside world--changing to extensionless broke all those links. Doh! Should've left well enough alone.
I would personally use the URL Rewriter. You can Redirect ASP.NET Legacy URLs to Extensionless with the IIS Rewrite Module.
So for example you could create an outbound rules that looked something like (not exactly, they definitely need testing):
<outboundRules>
<rule name="remove .mvc outbound 1">
<match serverVariable="RESPONSE_LOCATION"
pattern="^/(.*).mvc$" />
<action type="Rewrite" value="/{R:1}" />
</rule>
<rule name="remove .mvc outbound 2">
<match filterByTags="A, Form, IFrame, Img, Input, Link, Script"
pattern="^/(.*).mvc$" />
<action type="Rewrite" value="/{R:1}" />
</rule>
</outboundRules>
If someone decided to hardcode an MVC extension...
<rule name="Add Slash Inbound">
<match url="(.*)" />
<conditions>
<add input="{PATH_INFO}" pattern="^/(.*).mvc$" negate="true" />
</conditions>
<action type="Rewrite" url="{R:0}/" />
</rule>
Looks like here is coded your solution with adding / at end of each requests Add a trailing slash at the end of each url?. If it is not enough We can try something else :)
For anyone trying to figure this out: the problem may not be that RedirectToAction is generating a bad URL, but actually that the URL is not being interpreted properly by IIS.
I discovered that other servers had no problem with the missing slash in the URLs described in the original question.
This led me to look at the App Pool setting. One setting in particular seems to affect this: "Enabled 32-bit applications". Setting this to true on the Application Pool enabled my application to parse URLs which were missing the trailing '/'.
Weird huh?

ASP.NET MVC 4 - 301 Redirects in RouteConfig.cs

How can I add a route to the RouteConfig.cs file in an ASP.NET MVC 4 app to perform a permanent 301 redirect to another route?
I would like certain different routes to point at the same controller action - it seems a 301 would be best practice for this, specially for SEO?
Thanks.
You have to use RedirectPermanent, here's an example:
public class RedirectController : Controller
{
public ActionResult News()
{
// your code
return RedirectPermanent("/News");
}
}
in the global asax:
routes.MapRoute(
name: "News old route",
url: "web/news/Default.aspx",
defaults: new { controller = "Redirect", action = "News" }
);
I know you specifically asked how to do this on the RouteConfig, but you can also accomplish the same using IIS Rewrite Rules. The rules live on your web.config so you don't even need to use IIS to create the rules, you can simply add them to the web.config and will move with the app through all your environments (Dev, Staging, Prod, etc) and keep your RouteConfig clean. It does require the IIS Module to be installed on IIS 7, but I believe it comes pre installed on 7.5+.
Here's an example:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect t and c" stopProcessing="true">
<match url="^terms_conditions$" />
<action type="Redirect" url="/TermsAndConditions" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

correct non www users to full www domain name in ASP.Net MVC

what is the best way to redirect site users who do not enter the www in a domain name to actually get the www site.
IE: I goto Google.com and I am redirected to www.Google.com
The most common way of dealing with this is to do a redirect when the URL isn't what you expected. Typically this is done using some sort of mod_rewrite module.
In ASP.NET MVC, you would have to catch the incoming request as early as you can in the request lifecycle, check the URL and then redirect (response code 301 or 302) to the correct URL if need be.
I found sample code from this blog post: Canonical urls With ASP.NET MVC. It demonstrates one way of accomplishing this:
protected void Application_BeginRequest(Object sender, EventArgs e)
{
if (Request.Url.Authority.StartsWith("www"))
return;
string url = (Request.Url.Scheme
+ "://www."
+ HttpContext.Current.Request.Url.Authority
+ HttpContext.Current.Request.Url.AbsolutePath
);
Response.Clear();
Response.Status = "301 Moved Permanently";
Response.AddHeader("Location", url);
Response.End();
}
For those of us doing things with Mono and Apache or using one of the mod_rewrite extensions to IIS, here is a mod_rewrite example:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com$1 [R=301,L]
Following these suggestions about having an A record or CNAME only allow people to access the site from both domains, but it reduces your Google Juice because you have the same content at two different URLs.
If you're using the Microsoft IIS URL Rewrite Module then the following entry in your web.config will keep your domain consistent.
<rewrite>
<rules>
<rule name="Consistent Domain" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<conditions>
<add input="{HTTP_HOST}" pattern="^mediapopinc.com$" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="http://www.mediapopinc.com/{R:1}" />
</rule>
</rules>
</rewrite>
If this is to be handed correctly it should be done as an alias on the http server, not in your code...imo.
So when the server is contacted without the www, it should just redirect it to the correct URL.
Should not ignore the 301 redirections.
There is a nice post written by shahed khan to deal with that kind of situation.
http://dotnetusergroup.com/blogs/shahedkhan/archive/2009/05/18/10229.aspx
This is best handled via an A record (or CNAME record) on the DNS server that manages your domain. You can handle this in code but it really isn't the proper place for it.

Resources