I want covert aspx page into html at runtime for SEO.
like example
localhost:45556/index.aspx => localhost:45556/index.html
Google wouldn't care if it's aspx or html. The more important part is that the domain name tells something about what the site is about, and the URL path tells something about the page you are on.
www.domain.com/shirts/tshirts/green/ is a better URL than www.domain.com/prodId=3999944
You could use the IIS Rewrite Module like described in this post:
Remove HTML or ASPX Extension
and change the match URl criteria to
<match url="(.*).html" />
and change the action to the following
<action type="Rewrite" url="{R:1}.html" />
You can do this in c#.NET to change the .aspx to .html
Please put this code in your Global.asax file.
protected void Application_BeginRequest(object sender, EventArgs e)
{
HttpApplication app = sender as HttpApplication;
if (app.Request.Path.ToLower().IndexOf(".html") > 0)
{
string rawpath = app.Request.Path;
string path = rawpath.Substring(0, rawpath.IndexOf(".recon"));
app.Context.RewritePath(path+".aspx");
}
}
Related
I am writing an e-commerce website using DotNetNuke, and I have ran into a problem. For example I have a module on a page that has a URL of mydomain/productType/product-pages. What I would like is to pass a query string to this page with the item number of product (lets say its name is bacon). And when page loads, I would like both the breadcrumbs and URL(at browser) to read mydomain/productType/product-pages/bacon. I have researched how to change the page title, meta description, and all that already and have tested and it works - but just cannot find a way to modify the URL. I don't even know if this is possible. My goal is to not create all the pages for products within DNN, because this will change over time. I'm pretty sure I can create a page within DNN each time page is passed the query string which is a possibility, and another possibility would be have my other module create the link like it should read(just no page created) and DNN would just land on product-pages just add the /bacon? But I would rather just spoof the URL if possible.
Any suggestions or help would be greatly appreciated, and Thanks for reading.
Below is code snippet for changing the title and description:
protected void Page_PreRender(object sender, EventArgs e)
{
string pageName = Request.QueryString["pageName"];
if (!string.IsNullOrEmpty(pageName))
{
Page.Title = pageName;
Page.MetaDescription = "Blah";
Page.MetaKeywords = "Stuff,more stuff";
var url = HttpContext.Current.Request.RawUrl;
//Page.ResolveUrl(url + "/" + pageName);//this didnt work
//below is another way compared to top
//DotNetNuke.Framework.CDefault myPage = new
DotNetNuke.Framework.CDefault();
//myPage = (CDefault)this.Page;
//myPage.Title = "This is the new title";
}
}
The best way to manipulate the URLs for your custom module is by building a Extension URL provider. But you need to reverse your thinking about the problem. You don't want an ugly URL with a querystring argument to change into another URL. Rather, you start with the desired URL path and you want that to resolve or be "written" as the ugly querystring URL under the covers. That's what a Extension URL provider does.
I have a tutorial on DNNHero.com that walks you through it.
https://www.dnnhero.com/video/introduction-and-url-rewriting-basics
Unfortunately, that video is behind a paywall. (IMHO, it is worth the cost even for this one tutorial and code.)
You can also check out the blog:
https://www.dnnsoftware.com/wiki/extension-url-providers#:~:text=An%20Extension%20URL%20Provider%20is,and%20logic%20to%20be%20implemented.
Let me explain. Recently, I migrated a classic asp application to .net MVC4. The old site was made up of a lot of html and aspx pages, i.e:
index.html
book.html
contactus.aspx
naturally, the old asp site was accessed by urls like www.library.com/index.html, www.library.com/book.html, www.library.com/contactus.aspx, etc.
Now, the current MVC site is accessed via routers, i.e., www.library.com/Home, www.library.com/Book, www.library.com/Contact.
A lot of company customer try to access the new site using the old URLs, this is a problem, i need find a solution from code.
I try some solutions, but don't work.
Solution One
For old aspxs page, it's possible create aspx pages on the new site root with the same name that the old site and redirect on Page_Load from codebehind
//Create contactus.aspx on root directory project and redirect
public partial class Contact : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.RedirectToRoute(new { controller = "Contact", action = "Index" });
}
}
For html page you can define a simple refresh on meta tag
//Create a html page, i.e., index.html
<meta http-equiv="refresh" content="0; url=http://library.com/Home" />
<p>Redirect</p>
This it's not a viable solution, it's necesary create a html page or aspx page for each needed redirect.
Solution 2
I've try define a redirect conditional on Global.asax, but doesn't work
protected void Application_BeginRequest(object sender, EventArgs e )
{
string url = Request.Url.ToString().ToLower();
if (url.Contains("index.html"))
Context.Response.RedirectPermanent("/Home");
else if (url.Contains("book.html"))
Context.Response.RedirectPermanent("/Book");
else if (url.Contains("contactus.aspx"))
Context.Response.RedirectPermanent("/Contact");
}
Solution 3
I've try define maps for redirect on RouteConfig.cs, but doesn't work too
//Trying redirect on RegisterRoutes() in RouteConfig.cs
routes.MapRoute(
name: "Home",
url: "index.html",
defaults: new { controller = "Home", action = "Index" }
);
routes.MapRoute(
name: "Book",
url: "book.html",
defaults: new { controller = "Book", action = "Index" }
);
routes.MapRoute(
name: "Contact",
url: "contactus.aspx",
defaults: new { controller = "Contact", action = "Index" }
);
How redirect to new mvc routes when user try to access old asp site url correctly? need find a quickly and economic solution, thanks for help.
You need Url Rewrite based on Regular Expressions Pattern. Refer this thread, OP (#Eilimint) has updated the solution inline. This could be an elegant solution that allows us to override without changing the code.
<rule name="Redirect to new domain" stopProcessing="true">
<match url="^(.*)$" />
<conditions>
<add input="{HTTP_HOST}" matchType="Pattern" pattern="^olddomain(:\d+)?$" />
</conditions>
<action type="Redirect" url="https://my.newdomain.io/{R:1}" redirectType="Permanent" />
</rule>
I've struggled with a solution to redirect old html pages to asp.net routing for years. Solution 3 works well when the pages are at the root level, but not so easy in sub folders. Also, depending on the structure of the new site, one route redirection may not cover all html pages.
I finally surrendered to creating the original html page with a meta http-equiv="refresh":
Original Page: https://oldsite.com/pages/subpages/old.html
New Page: https://newsite.com/pages/subpages/old.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="refresh" content="7; url='https://newsite.com/newcontroller/action'" />
</head>
<body>
<p>Please follow <a ref="https://newsite.com/newcontroller/action">this link</a>.</p>
</body>
</html>
I know it has been a while since this question was asked, but I had the similar requirements. Just posting in case anyone comes looking.
I needed to redirect aspx pages (when accessed directly) to their friendly names configured in RouteTable collection. I got it to work by calling following code in Application_BeginRequest of global.asax.vb
If Request.FilePath.IndexOf(".aspx", StringComparison.OrdinalIgnoreCase) > -1 Then
Dim rh As PageRouteHandler
For Each r As Route In RouteTable.Routes.OfType(Of Route)
Try
If TypeOf (r.RouteHandler) Is PageRouteHandler Then
rh = r.RouteHandler
If rh.VirtualPath.Contains(Request.FilePath) Then
Response.Redirect(r.Url)
End If
End If
Catch ex As Exception
End Try
Next
End If
I want to remove ".aspx" from my web appliaction url. also I have using webservices.
If I use below code web services is not working.
Global.asax
protected void Application_BeginRequest(object sender, EventArgs e)
{
String WebsiteURL = Request.Url.ToString();
String[] SplitedURL = WebsiteURL.Split('/');
String[] Temp = SplitedURL[SplitedURL.Length - 1].Split('.');
// This is for aspx page
if (!WebsiteURL.Contains(".aspx") && Temp.Length == 1)
{
if (!string.IsNullOrEmpty(Temp[0].Trim()))
Context.RewritePath(Temp[0] + ".aspx");
}
}
for Eg:-
Actual page is DEFAULT.aspx, but I want to show DEFAULT in address bar. So I used Global.asax to remove (.aspx). It's working fine. but Web service is not working(Default.asmx)
There is a module that will handle this for you without having to directly manipulate the urls, as described here: http://www.hanselman.com/blog/IntroducingASPNETFriendlyUrlsCleanerURLsEasierRoutingAndMobileViewsForASPNETWebForms.aspx.
Install the package, Microsoft.AspNet.FriendlyUrls.
In your RouteConfig, the extensionless urls are enabled using:
routes.EnableFriendlyUrls();
You can generate friendly urls using extension methods, for example, to generate /Foo/bar/34, you can use:
Click me
I have some pages in /panel/ directory and some in /website/ directory. I need two different 404 pages for these pages based on their path, because these directories have different master pages.
when I use customErrors in web.config file, it redirects all pages to one 404 page so it's useless for me.
How can I handle it? Can I handle it in global.asax or master pages or what?
Thanx in advance.
You can either create smaller web.config on those locations with the settings you want to override, or on the root web.config you specify the multiple customErrors settings inside location tags.
You don't need to change any code for this.
You want to handle errors in the Global.asax file:
void Application_Error(object sender, EventArgs e)
{ Exception exc = Server.GetLastError();
if (exc is HttpUnhandledException)
{ // Pass the error on to the error page.
Server.Transfer("ErrorPage.aspx?handler=Application_Error%20-%20Global.asax", true); } }
Check out this link for more info
How can i change view like "www.abc.com/welcome" in browser but actual path is "www.abc.com/welcome.aspx".
And when i type "www.abc.com/welcome" then will go path "www.abc.com/welcome.aspx" but still view like "www.abc.com/welcome".
I have try this code on web.config below but got error:Unrecognized configuration section urlMappings
<urlMappings enabled="true">
<add url="~/welcome.aspx" mappedUrl="~/welcome" />
</urlMappings>
I wonder still got other way?
Where did you get the information about this urlMappings section? It's not supported by default by IIS or ASP.Net.
I think you might want to look at the UrlRewrite Module.
With this it's trivial to setup url rewrites like the one you want.
If you're using a URL rewriting module, you need to make sure which version of IIS you will be running on before something like "/welcome" will work. IIS6 doesn't support extensionless URLs by default. You'll need to have an ISAPI filter running for it, or you'll need to run on IIS7.
I suggest you to use routing How to: Use Routing with Web Forms.
You will need to register the UrlRoutingModule and the UrlRoutingHandler handler to be able to use routing feature (more details could be found in the article above).
And then in global.asax
void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.Add("BikeSaleRoute", new Route
(
"bikes/sale",
new CustomRouteHandler("~/Contoso/Products/Details.aspx")
));
}