Rewrite Url with out extension - asp.net

I am using Intelligencia for my url-rewrite functionality.
<configSections>
<section name="rewriter" requirePermission="false"
type="Intelligencia.UrlRewriter.Configuration.RewriterConfigurationSectionHandler, Intelligencia.UrlRewriter"/>
</configSections>
The below rule its working fine for my search page
<rewrite url="~/search/(.+).aspx" to="~/search.aspx?type=$1"/>
but i need to write my search page with below rule
<rewrite url="~/search/(.+)" to="~/search.aspx?type=$1"/>
If i run with above rule i am getting page not found error.
Plz advice me is there any setting in IIS.

Here is an article (with a downloadable solution sample) to use Routes to map to extensionless "SEO Friendly" urls:
http://goo.gl/BBnsa

Related

How can we allow dots in ASP.NET MVC urls?

I have a website developed in MVC 5 that perform search in inventory and the url is like this
http://localhost:56099/search/SQUARE
This url works, it redirects to Search controller and Index action with search query as SQUARE and gives the correct result. But, if I enter 2 dots as a search query it just takes me to my root page. The url will be like this
http://localhost:56099/search/..
it's strange because same thing works when passing single dot or multiple dots, so I can't find any technical reason why it is getting neglected.
I have done following things in Web.Config:
<modules runAllManagedModulesForAllRequests="true"> for accepting others characters also in search query.
relaxedUrlToFileSystemMapping="true"
But no success and I can't find any real reason for this weird behaviour. Any advice.
you should register the search route in your "RouteConfig.cs" file
routes.MapRoute(
"SearchRoute",
"Search/{*pathinfo}",
new { controller = "Search", action = "ActionName"});
You have the problem with .., because .. in your case is detected as relative URL, which indicates moving up to the parent directory(or root in your case), essentially stripping off everything up to the previous slash in the the Base URI.
UrlRoutingHandler in web.config should help you.
<system.webServer>
<handlers>
<add name="UrlRoutingHandler"
type="System.Web.Routing.UrlRoutingHandler,
System.Web, Version=4.0.0.0,
Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a"
path="/Search/*"
verb="GET"/>
</handlers>
</system.webServer>
Then every URL starting with /Search will be considered as MVC URL.
Also you could try:
<system.webServer>
<security>
<requestFiltering allowDoubleEscaping="true" />
</security>
</system.webServer>

Dynamic URL REWRITING for QueryStrings

hello please help me for this question
i have the following url --> www.sample.com/news.aspx?id=45
i want pass "id" in the query string to news.aspx and show this news, but due to url rewriting the url is changed to this --> www.sample.com/news/my-news-45/
How to extract "id" from the query string?
Thanx for your help
you can manually done URL rewriting but The downside of manually writing code can be tedious and error prone. Rather than do it yourself, I'd recommend using one of the already built HttpModules available on the web for free to perform this work for you.
Here a few free ones that you can download and use today:
http://urlrewriter.net/
http://www.urlrewriting.net/149/en/home.html
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="rewriter"
requirePermission="false"
type="Intelligencia.UrlRewriter.Configuration.RewriterConfigurationSectionHandler, Intelligencia.UrlRewriter" />
</configSections>
<system.web>
<httpModules>
<add name="UrlRewriter" type="Intelligencia.UrlRewriter.RewriterHttpModule, Intelligencia.UrlRewriter"/>
</httpModules>
</system.web>
<rewriter>
<rewrite url="~/products/books.aspx" to="~/products.aspx?category=books" />
<rewrite url="~/products/CDs.aspx" to="~/products.aspx?category=CDs" />
<rewrite url="~/products/DVDs.aspx" to="~/products.aspx?category=DVDs" />
</rewriter>
</configuration>
The HttpModule URL rewriters above also add support for regular expression and URL pattern matching (to avoid you having to hard-code every URL in your web.config file). So instead of hard-coding the category list, you could re-write the rules like below to dynamically pull the category from the URL for any "/products/[category].aspx" combination:
<rewriter>
<rewrite url="~/products/(.+).aspx" to="~/products.aspx?category=$1" />
</rewriter>
the complete reference can be found on this linke
http://weblogs.asp.net/scottgu/archive/2007/02/26/tip-trick-url-rewriting-with-asp-net.aspx

asp.net url rewriting with IIS integrated pipeline mode

I am new to ASP.NET. I created a web application and decided to use url rewriting. I tried several solutions like http://urlrewriting.net and http://urlrewriter.net/index.php/support/configuration
These solutions worked fine on my localhost. But when I uploaded it to a shared hosting service provider, all my web pages get 500 internal server errors.
THe web hosting provider told me HttpModules and HttpHandlers are incompatible with IIS Integrated Pipeline mode. They said I'm supposed to move my settings into system.webServer...I tried doing that but must have messed up somewhere because I now get 404 errors. Can someone tell me how to get url rewriting to work for my scenario? Here's what my original web.config looks like:
<configSections>
<section name="urlrewritingnet"
restartOnExternalChanges="true"
requirePermission ="false"
type="UrlRewritingNet.Configuration.UrlRewriteSection,
UrlRewritingNet.UrlRewriter" />
</configSections>
<system.web>
<compilation debug="true" targetFramework="4.0"></compilation>
<httpModules>
<add name="UrlRewriteModule"
type="UrlRewritingNet.Web.UrlRewriteModule, UrlRewritingNet.UrlRewriter" />
</httpModules>
</system.web>
<urlrewritingnet
rewriteOnlyVirtualUrls="true"
contextItemsPrefix="QueryString"
defaultPage = "default.aspx"
defaultProvider="RegEx"
xmlns="http://www.urlrewriting.net/schemas/config/2006/07" >
<rewrites>
<add name="Rewrite" virtualUrl="^~/([^\/]+)/(\d+)$"
rewriteUrlParameter="ExcludeFromClientQueryString"
destinationUrl="~/$1.aspx?id=$2"
ignoreCase="true" />
<add name="Rewrite" virtualUrl="^~/(search|administrator|Default|logout)$"
rewriteUrlParameter="ExcludeFromClientQueryString"
destinationUrl="~/$1.aspx"
ignoreCase="true" />
</rewrites>
</urlrewritingnet>
I think what they are saying is
1) You have to use the rewriter in .NET
2) You have to set it up to use the URL Rewriter, which sits under system.webServer, not system.web.
If I am correct, they are using the URL Rewriter: http://www.iis.net/download/urlrewrite
NOTE: They may not allow your custom HTTP handler (yes, I know it is a published third party, but ISPs are funny like that).

Regular expression : need help for url rewriting

I am using urlrewriter.net
my intended url is like /articles/3/name_of_article/articles.aspx
& actual is articles.aspx?article =3;
(3 is just taken, it can be any number)
i am using regex like this
<rewriter>
<rewrite url="^/articles/(.+)/(.+)" to="/articles.aspx?article=$1" />
</rewriter>
it does not work, also if i delete module dll from references then also no exception is
thrown.
1) how can ensure that module is loaded (via code)?
2) is my regex correct?
my web.config contains this:
<configSections>
<section name="rewriter" requirePermission="false"
type="Intelligencia.UrlRewriter.Configuration.RewriterConfigurationSectionHandler,
Intelligencia.UrlRewriter" />
</configSections>
&
<httpModules>
<add type="Intelligencia.UrlRewriter.RewriterHttpModule,
Intelligencia.UrlRewriter" name="UrlRewriter" />
<add name="ScriptModule"
type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</httpModules>
2) is my regex correct?
No, you should probably change it to ^/articles/([^/]+)/.+$, otherwise the first capture will gobble up "3/name_of_article" and not just "3", and you don't need the second capture group. You can also write it with a non-greedy match in the capture group, e.g. ^/articles/(.+?)/.+$.
This doesn't answer your question at all, but thought you might like to know. I had used both UrlRewriter and ISAPI_Rewrite, and ISAPI_Rewrite was much better in my opinion.
You don't have to include any references or rewrite rules in your web.config. Rather you install it as an ISAPI extension in IIS and it has it's own config file, and the rules you write are almost identical to the Apache mod_rewrite module, so if you are familiar with that module it is another advantage.
Also, since it is executed at the web server level before being passed to the .NET framework, it doesn't need to be tied into the ASP.NET request life cycle.
You can check it out here.
ISAPI_Rewrite 3

Alternate Web.Config for IIS7 Rewrite Module Rules

Is it possible to move the rules created by the IIS7 rewrite module from the root web config into its own web config file like you can with appsettings and if so how?
I can't seem to get it working, but the way it's described is:
<rewrite>
<rewriteMaps configSource="external.config">
</rewriteMaps>
</rewrite>
Then in the external.config file add your rules:
<rewriteMaps>
<rewriteMap ...
...
</rewriteMaps>
You have to do this with the entire rewriteMaps section: according to this forum post, you can't do this with the rewriteMap: http://forums.iis.net/t/1154113.aspx
This works for me in web.config:
<system.webServer>
<rewrite>
<rules configSource="web.rules.config" />
</rewrite>
</system.webServer>
One cool thing is that the IIS Configuration Editor respects this external file when you edit the rules and writes the changes back to the external file.
If you put:
<system.webServer>
<rewrite configSource="web.rules.config" />
</system.webServer>
it won't work, you get HTTP error 500.19 Internal server error:
Error Code: 0x8007000d
Config Error: Unrecognized attribute 'configSource'
Can anyone point to the definitive MSDN help page on the rewrite element and the configSource attribute? The MSDN article on system.webServer does not mention the rewrite element and I can't find an MSDN page via google.
I found configuration documentation but it doesn't seem to cover use of the configSource attribute.
My guess is that the <rules /> element is implemented as a SectionInformation which has a configSource property while the <rewrite /> element is a ConfigurationSection which does not.

Resources