ASP.NET page redirection list - asp.net

For SEO reasons we are about to face a major refactoring for a ASP.NET website. To please SEO engines most of .aspx pages will be renamed. The problem is that these pages are referenced all over the web and we absolutely need to avoid to break these links, nor the SEO ranking boost that comes from these links.
Three questions:
A) Is there a common way to handle this?
B) I'd like to centralize in a C# file or an ASP.NET or IIS config file, a redirection table, something defined like:
{ "Page1OldName.aspx", "Page1NewName.aspx" }
{ "Page2OldName.aspx", "Page2NewName.aspx" }
{ "Page3OldName.aspx", "Page3NewName.aspx" }
...
I am aware of the Response.Redirect() method. I can imagine an ugly way to intercept page-not-found error and then use Response.Redirect() coupled with the redirection table, but it doesn't look clean. Is there a cleaner way?
C) Do such redirection could/will provoke SEO ranking penalty?

you can try to use UrlMapping in webconfig.
Test001 is the old url.
Test002 is the new.
<system.web>
<urlMappings>
<add url="~/test001.aspx" mappedUrl="~/test002.aspx" />
</urlMappings>
</system.web>

Related

Rebuilt My .ASPX Website But How to Easily Redirect to .NET Core Pages [on Shared Hosting]?

I know this has been asked before but I haven't seen any simple explanations and I should also add I'm on shared hosting (Plesk). I don't see the URLRewriter utility installed on the server.
Anyway, I rebuilt my 2013 website that did use ASP.NET web forms (with .ASPX extensions). I'd like to be able to redirect my old pages to their new equivalents. i.e.
https://www.findaforum.net/diyfishkeepers-com.aspx
Should now point to:
https://www.findaforum.net/Forums/diyfishkeepers-com/
At the moment the .ASPX pages show this in a red box on a yellow screen:
XML Parsing Error: no element found
Location: https://www.findaforum.net/diyfishkeepers-com.aspx
Line Number 1, Column 1:
Where does this "come" from?
Incidentally, I'm looking for a quick and easy fix because I don't have too many external links pointing to my site's subpages, but it would be nicer for the user experience to fix it while Google works out I've changed my entire site.
This answer basically works:
How to redirect .ASPX pages to .NET Core Razor page
There's a good link to the URL rewriting regular expressions here: https://isitoktocode.com/post/introduction-to-url-rewriting-using-iis-url-rewrite-module-and-regular-expressions
This is what I've put in Startup.cs:
var options = new RewriteOptions()
.AddRedirect(#"ShowCategories.aspx", "/Home/Categories/")
.AddRedirect(#"Statistics.aspx", "/Home/TopForums/")
.AddRedirect(#"SubmitForum.aspx", "/Home/Submit/")
.AddRedirect(#"Help.aspx", "/Home/HelpAndFAQ/")
.AddRedirect(#"([0-9a-z-A-Z]+)(.aspx)", "/Forums/$1/");
app.UseRewriter(options);
Note: Make sure you put the specific ones at the top, then the generic ones at the bottom.
I also had some URLs like https://www.findaforum.net/lpsg-com where 'lpsg-com' is a forum name. I added a URL to the home controller to take care of these...
[HttpGet]
[Route("{code}")]
public ActionResult Unknown(string code)
{
return RedirectToAction(code, "Forums");
}

How to make a URL based If Then statements

I have a client who has a huge site that would better serve their visitors if split up into two separate sites. So now I'm trying to split it up, while still sharing certain files between the two (essentially, the only thing that changes is the navigation and pages; the header, footer, and styling should all be the same between the two sites).
So now I am trying to create an If Else statement in ASP.net and VBScript that will switch out certain elements based on what the site's root file path/url is going to be. So far, it looks something like the following:
<% If objPage.FilePath = "/" Then %>
Show Content A
<% Else %>
Show Content B
<% End If %>
Which doesn't really help me as it only seems to effect the home page and not the other site pages. How might I change it to trigger based off the site address or, failing that, is there a variable I can stick in the quotes that will affect not just the root files, but all the subpage files as well?
Also, is there a better way to be going about this?
How about have a web.config setting which defines the flavour ?
For example:
<appSettings>
<add key="SiteFlavour" value="site1" />
<!--<add key="SiteFlavour" value="site2" />-->
</appSettings>
Then simply replace your conditional
objPage.FilePath = "/"
with a test based upon the config setting ?
This has the additional benefit of making testing easier, as you're not reliant upon a specific URL.
It also makes adding a third flavour quite straight forward.
You can always pull out the setting from config in your app start up (global.asax ?) if that helps.

vb.net CDN Images Only on Production

Whats the right way to change the url for all the images on my site to use a cdn url or not based on a web.config value.
I have this web.config value
<add key="UseCDN" value="1"/>
now my page has a whole bunch of <\asp:image imageurl="RELATIVEPATH" tags.
I want them to point to my machine when "useCDN" = 0 and to cdn.com\RELATIVEPATH when "useCDN" = 1
whats the best way to do this?
For you to implement a cross cutting solution, you have to extend the image control class and override the Render method to use the use the CDN value (if in production).
Or just create a normal ASCX user control and use it. Can't search now but a simple search will get you plenty of tutorials.
UPDATE:
A tutorial to help you do it

asp.net mvc how to manage urls/links and routing centrally (c# + js)

I keep running into problems with URLs and routing.
Couldn't find an answer on SO.
I would like to manage all of my urls/links in a single place.
This is for my C# MVC code and the js/jquery ajax code.
These urls are scattered throughout my application.
Moving to a production server needs some fixes and I don't like the fact that I need to look for all of the occurrences in the application.
I don't mind fixing this once - but I would like to do it only once.
Any ideas how to manage all of these links/urls as a group will be very appreciated.
Be happy ad enjoy life, Julian
Consider using T4MVC
You could use Html.ActionLink or
Html.BuildUrlFromExpression(c => c.ControllerAction())
Depends, if you have application reading off certain urls and those urls changed once in a while. then you might want to consider putting all those urls into a database table/etc and retrieve them using specific key.
that way, when your url changed, all you need to do is to change the url on your database and all your application will still be running fine.
Urls should be managed in a single place: the RegisterRoutes static method in Global.asax. In absolutely every other part of your application you should use Html helpers when dealing/generating urls. This way you will never have problems because helpers take into account your routing system.
So instead of writing:
$('#foo').click(function() {
$('#result').load('/mycontroller/myaction');
return false;
});
you use an HTML helper to generate this foo:
<%: Html.Action("foo", "myaction", "mycontroller") %>
and then:
$('#foo').click(function() {
$('#result').load(this.href);
return false;
});
Never hardcode a single url in your application except of course in global.asax which is the only centralized place urls should be defined. So basically every time you find yourself writing something of the form /foo/bar in some other part than global.asax you are doing it wrong.

How do I determine if an asp.net url has been "rewritten"?

I'm using http://urlrewriter.net/ to rewrite urls at my website. For example, I'm rewriting:
http://www.example.com/schedule.aspx?state=ca
to
http://www.example.com/california.aspx
What I'm trying to do (for SEO purposes) to to dynamically add the meta tag:
<meta name="robots" content="noindex,follow" />
only to the page that hasn't been rewritten. This is because I want both URLs to work, but only the rewritten one to be indexed by search engines.
How do I determine which version of the page has been requested?
EDIT
Answers below suggest a 301 redirect instead of using a meta tag. Maybe I'll do this, but I still want to know the answer to the underlying question... how do I know if the page has been rewritten?
personally, I would 301 redirect from the un-rewritten one to the re-written one, and only use the single copy of the page. It is easier for users, and from an SEO perspective, you have 1 copy of the content.
If you need to do this you can probably do something like:
<add header="X-WasRewritten" value="true" />
And you can check for the header in your view and add the robots meta tag if you need it.
This will get returned to the client too, so if you want to hide that you can write a CustomAction (http://urlrewriter.net/index.php/support/reference/actions/custom-action) which will set some kind of state value in your request.
However, having two URIs for the same resource is something I would not recommend. I suggest you just keep the one representation. If you're worried about invalidating old bookmarks you can set the old one to redirect to the new one.
Further to chakrit's answer, it looks like UrlRewriter.NET stores the original URL in the HttpContext, in a key called UrlRewriter.NET.RawUrl. So, you could try something like:
bool isPageRewritten =
!string.IsNullOrEmpty(HttpContext.Current.Items["UrlRewriter.NET.RawUrl"]);
Most obvious method is to use the Request.Url object in your page to get information about the URL and query string. For example:
if (Path.GetFileName(Request.Url.FilePath) == "schedule.aspx")
//Not rewritten
else
//rewritten
I think that's the job of HttpContext.Current.Items.
You can save the "Redirection" in HttpContext.Current.Items and then in your pages, you can check it for a certain added value.
I believe you can add hooks to urlrewriter.net that could do it, something alongs:
HttpContext.Current.Items["Redirected_From"] = currentUrlHere;
And then in your webpages, you could check it by:
if (!string.IsNullOrEmpty(HttpContext.Current.Items["Redirected_From"]))
// the page's been redirected, do something!
else
// no it's visited normally.
I have long since left it for the ASP.NET Routing framework in .NET 3.5 SP1, it is better than urlrewriter.net IMO.

Resources