Matching part of url and redirect - asp.net

I have a regular expression in my web.config file that I'm using to redirect users to some other domain:
<redirect url="/(.*/)?((da-DK)|(es-ES))/?$" to="http://www.example.com" />
This successfully matches following url:
http://www.example.com/ik/da-DK/
But not the below one:
http://www.example.com/da-DK/
Why is that? I'm certain that this regex is good because I've tested it against lots of example urls. Is this a bug in parser or something like that?
I'm using urlrewriter.net which is no longer maintained, but maybe any of you had such problems in the past?

Regex itself seems ok, so considering that urlrewriter.net is not actively maintained, I would suggest that you try to switch to IIS Url Rewrite:
http://www.iis.net/downloads/microsoft/url-rewrite

Related

Change the broswer's address name

how can I change the broswer's address name?? For example I have this: www.example.es/Pages/site.aspx and I want this: www.example.es/site
How can I do that?? Ty.
you can use the 404 error challenge and Server.transfer() to do URL rewriting using classic asp
Things you need
How to create a custom 404 page
Regular expression pattern to recreate actual file path or a predefined list
eg:-www.example.com/pg1/ ---> /pages/pg1.asp
Simple server.transfer() to handle valid / invalid server requests.
<%
IF Request.ServerVariables("SCRIPT_NAME") = "pg1" THEN
Server.transfer("/pages/pg1.asp")
ELSE
Response.write("404 - Page not found");
END IF
%>
that's all you are doing basic level URL rewriting using classic asp.
Your question asks about .aspx (asp.net) pages, but your question is tagged asp-classic... you will get better responses to future questions by using the correct tag.
In this case: as long as you can install a module on your server, and you're server can process ASP.Net's web.config files, you can use the IIS URL Rewrite module that Graham mentioned in the comments. I've got an old ASP-Classic site that uses the module just fine.
If you can install an extension, you could also use Ionics Isapi Rewrite Filter. Some hosts have this installed already.
All things being equal, if you have the ability to run your choice, I'd go with the IIS URL Rewrite Module as I've generally seen better performance from it. But that may just be me.
If you do not have that kind of access to the server, and there's not something already installd, you may have to go with the 404 mapping Jeevaka Nuwan Fernando mentions.
There are some other options, but they depend on if you are using ASP.Net or ASP-Classic.

Extensionless url rewriting / filtering url rewrites

I've done some url rewriting, always using the Intelligencia.UrlRewriter and it always worked fine. But now i want to achieve the following and somehow i can't get it right.
i want:
www.mydomain.com/products/books > rewrite to www.mydomain.com/products.aspx?id=books
this works fine, because i can set my criteria to /products/..
www.mydomain.com/mybook > rewrite to www.mydomain.com/productdetails.aspx?id=mybook
and of course i want www.mydomain.com/newbooks.aspx to function normally
and these last 2 don't work together.
is there a way i can tell my rewrite rule to only rewrite when there is no extension in my url? or is there an other trick?
Thanks in advance!
Well, a bit off the road, but try using ASP.NET Routing instead of URL Rewriter.
I was playing (and messing up) with the rewriter, and here on Stackoverflow I was advised to use Routing, and it is MUCH better (and easier when you get it).
You'll need some little changes to your ASPX files though, but very small.
I thought i'd solve this old, unanswered question. The problem with the extensionless urls turned out to be some configuration problem in IIS. Our system admin fixed this by allowing these urls and after that the rewriter had no problems picking up and translating the URLS.

subdomain rewriting in ASP.NET?

How do i do this in .NET? http://why.does.my.head.asplode.net/ I want to do something like ytmnd where each url is a different user generated page. I might need something as simple as pointing to a directory so dirname.mysite.com will redirect to http_public/userGenContent/dirname/
I suggest you use UrlRewritingNet. You may have to add a rule similar to this:
<add virtualUrl="http://(.*).asplode.net/index.aspx"
rewriteUrlParameter="ExcludeFromClientQueryString"
destinationUrl="~/Users.aspx?username=$1"
ignoreCase="true" />
I cannot test this since I'm on a Linux machine right now, but it should work ;).
HAHA ...
I handled the Begin request event in global.aspx for this because my urls come from sql server.
Why are so many people sub domain crazy these days?
in Begin Request you can do something like this (not usre exactly because I aint got it in front of me right now) ...
request.rewriteurl( "new url" );
... this will take whatever the source url is and "redirect" without a redirect the response to a url you can use internally.
The various UrlRewriting tools will help you once you have the request getting into ASP.NET, however before that happens you need to setup IIS to pass these requests to your app. This is simple enough if there's only one app on the server, but more difficult otherwise.
Check out http://msmvps.com/blogs/bernard/archive/2005/03/22/39218.aspx for some details on wildcard subdomains on IIS.
Alternatively use an IIS level rewriter like ISAPI Rewrite
Essentially search for IIS wildcard subdomain to find a wealth of approaches.
Well, IIS7 has built url rewrite functionality. You can specify rules etc in web.config. But for IIS6 you need ISAPI dll that does the same for you. I've used IIRF and it works just fine.

How do I redirect this url pattern in asp.net webforms or asp.net mvc?

I am trying to redirect a user when they entire a URL in this format:
http://example.com/http://example2.com/
To the following:
http://example.com/?m=example2.com#http%3A%2F%2Fwww.example2.com%2F
I want to strip out the host of example2 and make it a query-string, then URL encode the full URL of example2 and put it as the anchor.
What is the best way to do this in asp.net? I can use mvc, iis7, or iis6. I know I could do this by mapping everything to .NET in IIS6 and then using httpcontext.rewritepath but I bet there is a better way.
Thank you.
Unfortunately, ASP.NET does not allow a colon anywhere in the URL path. Not even an escaped colon (%3A). It is only allowed in the querystring portion of the path.
This restriction is in place even before URL rewriting happens. (You'll get the 400 Bad Request error.) In that question, the asker answered their own question with a link to Stefan from the ASP.NET team.
Of course, you can still collect the URL from your user any way you like, and format the URL or redirect in a way that ASP.NET can use.
What do you think about urlrewritting concept of ASP.net. I think it can solve your purpose

ASP.NET URL Rewriting

How do I rewrite a URL in ASP.NET?
I would like users to be able to go to
http://www.website.com/users/smith
instead of
http://www.website.com/?user=smith
Try the Managed Fusion Url Rewriter and Reverse Proxy:
http://urlrewriter.codeplex.com
The rule for rewriting this would be:
# clean up old rules and forward to new URL
RewriteRule ^/?user=(.*) /users/$1 [NC,R=301]
# rewrite the rule internally
RewriteRule ^/users/(.*) /?user=$1 [NC,L]
Microsoft now ships an official URL Rewriting Module for IIS: http://www.iis.net/download/urlrewrite
It supports most types of rewriting including setting server variables and wildcards.
It also will exist on all Azure web instances out of the box.
I have used an httpmodule for url rewriting from www.urlrewriting.net with great success (albeit I believe a much earlier, simpler version)
If you have very few actual rewriting rules then url mappings built in to .NET 2.0 are probably an easier option, there are a few write ups of these on the web, the 4guysfromrolla one seems fairly exhaustive but as you can see they don't support regular expression mappings are are as such rendered fairly useless in a dynamic environment (assuming "smith" in your example is not a special case then these would be of no use)

Resources