
There are cases when you need to optimize the links so that they are better indexed by search engines (for
SEO purposes and not only). Suppose when you need to
rewrite links that include the characters "?", "&" And "=" in a more readable form.
For example, you want to remake a link of the following form:
www.domain.com/default.aspx?category=Title&entry=NameIn this, more understandable:
www.domain.com/Title/Name')
There are several ways to achieve Url Rewriting in ASP.NET.
1. You can take advantage of what standard ASP.NET 2.0 gives you, that is, use the
urlMappings element. Add the following lines to the
Web.config file:
< system.web >
< urlMappings enabled = "true" >
< add url = "~ / Home.aspx" mappedUrl = "~ / Default.aspx? tab = home" />
</ urlMappings >
</ system.web > * This code was highlighted with Source Code Highlighter .
This method is simple and convenient, but here all links will be "
hardcoded ". Using this method your opportunities will be limited. You cannot dynamically rewrite links.
2. There is also an
Open Source module called
UrlRewritingNet.UrlRewrite . UrlRewritingNet is essentially a set of
DLL files. To work with them, you need to copy them to the
/ Bin / directory of your project.
Next, add the following lines to your project's
Web.config file:
< configuration >
< configSections >
< section name = "urlrewritingnet" requirePermission = "false" type = "UrlRewritingNet.Configuration.UrlRewriteSection, UrlRewritingNet.UrlRewriter" />
</ configSections >
< configuration >
< system.web >
< httpModules >
< add name = "UrlRewriteModule" type = "UrlRewritingNet.Web.UrlRewriteModule, UrlRewritingNet.UrlRewriter" />
</ httpModules >
</ system.web > * This code was highlighted with Source Code Highlighter .
and finally the link rewrite rules themselves:
< urlrewritingnet rewriteOnlyVirtualUrls = "true" contextItemsPrefix = "QueryString" defaultPage = "default.aspx" xmlns = " www.urlrewriting.net/schemas/config/2006/07 " >
< rewrites >
< add name = "Rule1" virtualUrl = "^ ~ /(.*)/ Detail (. *) & # 60 ; / strong > .aspx" rewriteUrlParameter = "ExcludeFromClientQueryString" destinationUrl = "~ / Default.aspx? language = $ 1 & id = $ 2 "ignoreCase =" true " />
.
.
.
</ rewrites >
</ urlrewritingnet > This is where the source code was highlighted.
As you can see from the example,
UrlRewritingNet allows you to dynamically rewrite links using regular expressions.
It is convenient to use
UrlRewritingNet in cases when you do not have dedicated hosting (that is, when you do not have the ability to rewrite URLs at the server level).
You can read more about all the features of
UrlRewritingNet here , and download the module itself
here .
3. And the most, in my opinion, the correct way to rewrite the URL will be according to the means of the
ISAPI Rewrite filter. This is a module for IIS that accepts standard rules written for
Apache .htaccess mod_rewrite . It must be installed as an
ISAPI Filter through
IIS on the site itself.
An example
.htaccess file for rewriting links:
RewriteEngine on
RewriteRule ^/(.*)/(.*)/(.*).aspx /Default.aspx?Title=$1&Description=$2&Topic=$3
In this example, we want to refer to the following link:
www.domain.com/Default.aspx?Title=A&Description=B&Topic=CThrough this, more readable and short:
www.domain.com/A/B/C.aspxAnd so, for example, you can overcome the devouring of traffic from your site by blocking the display of pictures from other sites that are physically on your site:
RewriteEngine on
RewriteCond %{HTTP:Host}#%{HTTP:Referer} ^([^#]+)#(?!http://\1).+
RewriteRule .*\.(?:gif|jpg|png) /block.gif [NC]
ISAPI Rewrite rewrites URLs at the
IIS level. Here your possibilities are almost unlimited. The capabilities of
ISAPI Rewrite are not limited to rewriting links.
About
ISAPI Rewrite filter is written in more detail
here , and you can download the free module
here . The difference between the free and paid versions is that in the free version all the rules are saved in one file and valid for
all sites under IIS. In the paid version of the same rules can be written for specific sites.
Useful articles on this topic:-
URL Mapping in ASP.NET 2.0-
Tip / Trick: Url Rewriting with ASP.NET-
A Complete URL Rewriting Solution for ASP.NET 2.0-
URL Rewriting in ASP.NETPS
This is my first more or less serious post on Habré, so do not judge strictly.
I will be glad to any amendments in the Russian language and not only. :)