开发者

SEO URL rewriting ASP.NET

开发者 https://www.devze.com 2023-04-10 00:53 出处:网络
I already have an ASP.NET Web Site I want to change my site to be more SEO url friendly. I want to change ex. this site:

I already have an ASP.NET Web Site

I want to change my site to be more SEO url friendly.

I want to change ex. this site: www.mydomain.aspx?articleID=5

to: www.mydomain/article/learningURLrewrite - articlename needs to be read from DB

How do I accomplish this?

I have already tried with some articles from Google which mentions IhttpModule without any luck.

My goa开发者_Go百科l is to have a class responsible for redirecting based on folderpath(like this):

string folderpath = "my folderpath" (could be articles, products etc.)
string id = Request.QueryString["id"].ToString();

if(folderpath.equals("articles"))
{
   string name = //find name from id in DB
   //redirect user to www.mydomain/article/name 
}

if(folderpath.equals("products"))
{
   string name = //find name from id in DB
   //redirect user to www.mydomain/products/name 
}

Also I want to remove the aspx extension


You can use routing with ASP.NET WebForms too.

The steps are:

  1. Add the route (or routes) at application start.

    //In Global.asax
    void Application_Start(object sender, EventArgs e)
    {
        RouteTable.Routes.MapPageRoute("My Routename", "{*name}", "~/Article.aspx");
    }
    
  2. Create the Article.aspx as a normal webform

  3. In the code for Article.aspx, you can access the url path like this:

    public void Page_Load(object sender, EventArgs e)
    {
        var thePath = RouteData.Values["name"];
    
        // Lookup the path in the database...
    }
    


This posting tells you exactly how to use asp.net 4's routing engine - give it a whirl - if you have a specific problem in implementing it let us know.

http://weblogs.asp.net/dotnetstories/archive/2011/01/03/routing-in-asp-net-4-0-web-forms.aspx

Since you need specific parameter usage, you can define the parameters to get sent to your page. For that see: http://msdn.microsoft.com/en-us/library/cc668177.aspx

and

How to: Access URL Parameters in a Routed Page


If you are using ASP.NET 4, then you should look into URL Routing. You would end up setting up custom routes like so:

routes.MapPageRoute(
      "View Article",               // Route name
      "Articles/{*ArticleName}",  // Route URL
      "~/Articles.aspx"      // Web page to handle route
   );

And you write out the new links like so:

Page.GetRouteUrl("View Article", new { ArticleName= NAMEFROMDATABASE });

Unfortunately I won't give you a summary of how to build your entire site, but 2 really good places to start are an article by Scott Gu, and one on 4 Guys.


If you are using .net 3.5 or less then you can use these

  1. http://urlrewriting.net
  2. http://urlrewriter.net

I use the second one, in all my projects made in .net 3.5

if using .net 4.0 then you can do these

  1. URL routing ( I think it does not support sub domain rewriting)
  2. URL Rewrite 2.0 ( works with IIS 7 only)

UPDATE

Add these lines below the appSettings tag

<rewriter configSource="URLRewriter.config"/>

Then create separate file named as URLRewriter.config

And in that you can write like this (Add processing stop for the files not to be rewritten i.e images and js, etc)

<rewrite url="^(/.+(\.gif|\.png|\.jpg|\.ico|\.pdf|\.css|\.js|\.flv|\.eot|\.svg|\.ttf|\.woff|\.txt|\.doc|\.docx|\.pdf|\.xls|\.xlsx|\.xml)(\?.+)?)$" to="$1" processing="stop" />
<rewrite url="~/article/([^/.]+)" to="~/articledetail.aspx?articlename=$1" />

Then you would get the article name in the query string like this

string articlename = Request.QueryString["articlename"];

And the menu or other location of the site where you want to put the link to article, you can add an the AppSettings so that later on if you want to change the url pattern you can change it easily from the configs only,

  <add key ="ArticalDetailsURL" value="/article/{0}" />

Then, in the page you can do like this

string articleName = "TestArticle";
lnkMenuLink.NavigateUrl = string.Format(ConfigurationSettings.AppSettings["ArticalDetailsURL"], articleName);

Thanks and Regards,

Harsh Baid

0

精彩评论

暂无评论...
验证码 换一张
取 消