Url Rewriting for images - asp.net

I'm trying to figure out how to enable short URL's for images on the website using Application_BeginRequest.
Currently, to open an image I have to use full URL like this:
http://mywebsite.com/ViewImage.aspx?album=123&id=456
I want the images to be accessible using short URLs:
http://mywebsite.com/123/456
ViewImage.aspx retrieves images dynamically from the database.
Assuming I should be using RewritePath method. But how exactly?

You can accomplish this with URL rewrite http://www.iis.net/downloads/microsoft/url-rewrite You create rules in the web.config to map urls to files.
We use this image rule for example, to map version url's to real images. You can create something simulair for your urls. In IIS you can test you rules when you install the module
<rule name="rewriteImgRule" stopProcessing="true">
<match url="^v[0-9\.]+/img/(.*)$" />
<action type="Rewrite" url="/img/{R:1}" />
</rule>

#Ivo's answer may well be the simplest but I personally prefer to avoid installing additional modules.
If you're using IIS7 (Windows 7/Server 2008 or later) you can use request handlers (.ashx files) then map various Urls to them...
See http://www.dotnetperls.com/ashx for a tutorial on setting one up.
Once you've got one working, you can examine the Request object to determine what Url was used exactly and extract the parameters any way you like

Related

URL rewriting makes all the links of same patterns after visiting any rewritten link in ASP.NET

I am using UrlRewriter to rewrite my urls in an ASP.NET application. Everything works fine and the work which I need to do is working ok.
I want to redirect ~/product/45/something to ~/show_product_details.aspx?current_prod=45
and it's working fine. But the problem is when I request ~/product/45/something, I am getting the page from ~/show_product_details.aspx?current_prod=45. After that when I click some link like ~/home.aspx, it again redirects me to ~/product/45/home.aspx.
Please suggest how to fix this. I am using this rule:
<add name="Gallery1" virtualUrl="^~/product/(.*)/(.*)"
rewriteUrlParameter="ExcludeFromClientQueryString"
destinationUrl="~/show_product_details.aspx?current_prod=$1"
ignoreCase="true" />
It sounds like the URLs in your ASP.NET application are not resolved to the root of the application, but rather are relative links, causing the folder structure to be taken into account.
If you are using ASP.NET Web Forms you need to ensure you either call ResolveUrl("~/home.aspx") when outputting the URL, or ensure that the hyperlink is runat=server, for example:
<a runat="server" href="~/home.aspx">Home</a>
OR:
<a href='<%# ResolveUrl("~/home.aspx") %>'>Home</a>
Another way you can resolve this is to have a base reference in your HTML, which tells the browser that ALL links on the page must be rooted at the specified path:
In the <head> section:<base href='http://www.yourwebsite.com/' />

Rewrite URLs for DNN

I have a site and already trying to do a little url rewritting but want to know if there is a solution to make it better
my current state urls look like this
http://snit.us/Home/tabid/314/CN/1/RE/23/United_States_Missouri.aspx
I want to change them to something more ordered so in my new scheme i want to do this
http://snit.us/Home/United_States/Missouri/default.aspx
obviously the point is that if i remove missouri from the new url it would find all United_States - etc etc etc
even better would be to have http://snit.us/Home/United_States/Missouri so its even less cluttered
I think your best bet would be to implement the free iFinity URL Rewriter for DotNetNuke. At the very least, you should be able to achieve the following:
http://snit.us/Home/CN/1/RE/23/United_States_Missouri
If you're using IIS 6, you'll want to enable extensionless URLs in IIS. If you're using IIS 7, you should not need to make any changes.
To set it up, you'll just need to add the DLL that iFinity provides to your site's bin directory and then follow the instructions that came with the download for configuring your web.config (make sure to make a backup copy of your web.config for safety and convenience).
Here is an example configuration that we use on dnnGallery.net:
<add name="Ifinity.FriendlyUrl" type="iFinity.DNN.Modules.FriendlyUrl.DNNFriendlyUrlProvider, iFinity.FriendlyUrlProvider" includePageName="true" regexMatch="[^\+a-zA-Z0-9 _-]" urlFormat="HumanFriendly" redirectUnfriendly="true" doNotRewriteRegex="(\.axd)|(/DesktopModules/)" doNotRedirect="SearchResults;" doNotRedirectRegex="[.]*(/logoff.aspx)" pageExtensionUsage="never" parameterHandling="ordered" checkForDupUrls="true" forceLowerCase="true" redirectWrongCase="false" replaceSpaceWith="_" redirectDefaultPath="true" />

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.

Best DotNetNuke url rewriting approach?

I would like to implement a URL rewrriter for DotNetNuke. Have questions as to whether this is a good or possible approach and any suggestions other developers may have.
Can I add a dynamic or static route for URL rewriter from the 'Pages'-> 'Add Page' section?
Let's say I'm creating a page called 'Events' underneath the 'Activities' menu item.
Can I write some custom code in DotNetNuke that will add a prompt on the add page screen that assumes I will want a static or dynamic route added for - site.com/Activities/Events Or if I name the page 'Event Calendar' then it would suggest 'site.com/Activities/Event-Calendar'?
I would like any feedback or suggestions on how I would accomplish this! Thank you.
If you're using DotNetNuke 4.8 or greater, you just need to enable Human Friendly Urls through the Web.Config.
To enable human friendly urls, replace:
<add name="DNNFriendlyUrl" type="DotNetNuke.Services.Url.FriendlyUrl.DNNFriendlyUrlProvider, DotNetNuke.HttpModules" includePageName="true" regexMatch="[^a-zA-Z0-9 _-]" />
with:
<add name="DNNFriendlyUrl" type="DotNetNuke.Services.Url.FriendlyUrl.DNNFriendlyUrlProvider, DotNetNuke.HttpModules" includePageName="true" regexMatch="[^a-zA-Z0-9 _-]" urlFormat="HumanFriendly" />
UPDATE:
For DNN 7.x and greater, this should now be:
<add name="DNNFriendlyUrl" type="DotNetNuke.Services.Url.FriendlyUrl.DNNFriendlyUrlProvider, DotNetNuke.HttpModules" includePageName="true" regexMatch="[^a-zA-Z0-9 _-]" urlFormat="advanced" />
Additional Detail: Enable Human Friendly Urls in DotNetNuke
Check this out - details here:
http://www.ifinity.com.au/Blog/Technical_Blog/EntryId/19/Rewriting-the-DotNetNuke-Url-Rewriter-Module/
Also, take a look at the existing FriendlyUrl module that's already included in DNN.
If you want to do this on the stock pages, you'll need to crack open the source for it, or write a module that does this independently.
To answer your question directly : yes, you can add some code to the DNN Page Settings page. However, you'll be modifying the core code and thus won't be able to easily upgrade your installation when the next DNN release is made.
A lot of people come to DNN and want to write a new Url Rewriter. The problem is not the rewriting : it's getting the DNN framework to generate the correct urls where you would like them - in the menus, in generated links within modules. To really change the rewriting scheme you need to replace/augment the Url Rewriting function, the friendly url generation function, and provide some sort of redirect capability in order to tie old and new Urls together.
Another suggestion is Page Blaster, if you are not wishing write your own code. Works well for the sites l use it on.
Several of these are great replies... One of the most important things to know if you are just learning about DNN now (2016 and above) is that the realm of URL Rewriting has changed over time within DNN.
Before, in DNN 4.8 and below you had to use an external module/provider such as:
PageBlaster or
DNN Masters SEO URL REWriter or
iFinity URL REWriter
all of those are mentioned in some of the answers above.
However, after DNN 4.9, iFinity URL ReWriter was incorporated into the DNN core and you'll find it available in DNN 5, 6, 7,and 8 in different levels of exposure to the interface. You still need to do some updates in the web.config, but much of it is done directly within the Page Settings now.
Any settings that are not within the main managers, you can do either in the web.config, or in the database, or you can look for a DNN module such as X3 DNN URL Management which taps into the database records of the integrated DNN url rewriter and helps manage settings (it is very similar to the old iFinity interface screens/options) but you don't need it, you can work directly, it's just helpful.

URL Rewrite from /default.aspx to /

I'm using the URL Rewriting.NET tool with IIS 6. I've got my default page content set for default.aspx in IIS. What I'm trying to do is have /default.aspx provide a 301 redirect to the root directory (www.example.com/default.aspx -> www.example.com). I've tried turning off default documents, to no avail.
What I'm hoping to do is use a couple of URL Rewriting.NET rules to accomplish this goal. Any thoughts?
EDIT:
Sorry, I forgot to clarify. If I redirect from /default.aspx to / with default documents turned on (I'd like to leave them on) then I get an infinite loop of default -> / -> default
In the end I wound up using IIS 7 with the URL Rewrite module, which allows you to do this redirect properly.
Edit :
The rule is
<rule name="Default Redirect" stopProcessing="true">
<match url="^default\.aspx$" />
<action type="Redirect" url="/" redirectType="Permanent" />
</rule>
you can do that with a separate rule for each folder, or you can use
<rule name="All Redirect">
<match url="^(.*\/)*default\.aspx$" />
<action type="Rewrite" url="{R:1}" />
</rule>
I came across this very problem a while back while trying to work out why some IIS installs would work redirecting the /default.aspx and some would degenerate into a terminal loop.
I found the answer was whether or not asp.net was 'wildcard' mapped to run all requests within IIS.
Put simply, if you have an out-of-the-box IIS setup, it will always append the default document onto any request for the site root. Thus example.com becomes example.com/default.aspx when you inspect the Request.Url in ASP.NET. Therefore if you detect this situation and try to redirect away and back to example.com, IIS does so, appends the /default.aspx and your code is caught in a loop of it's own making.
The exception to this is if you set up wildcard mapping so that all requests are processed through the asp.net pipeline. In this case, IIS no longer appends the default document onto each request at the Request.Url level. And thus you can do the redirect.
I put it all in this blog post : 301 Redirecting from /default.aspx to the site root - the final word - but this was written several years back and changes in IIS7 may have fixed the problem, as the currently accepted answer provides.
But if you're battling this problem, then looking at the wildcard mapping status is the right place to start.
I had the same problem. For those who wonder why anyone would want to do this, it's a question of SEO. If Google indexes your home page with and without the default.aspx at the end, the PageRank and link popularity will be split between the two URL's. Now, if you're experiencing this problem, and you're able to consolidate the two URL's then you may get a boost in search rankings. One more thing to keep in mind is that if you're going through the trouble, you MUST use a 301 redirect for Google to consolidate their index between two URL's. Otherwise your efforts will be futile.
This is a little too late since you've already solved this by upgrading to IIS7. But I'll just add that the only solution to this problem I've come up with for IIS6 is to add an ISAPI filter.
I documented the complete solution here...
http://swortham.blogspot.com/2008/12/redirecting-default-page-defaultaspx-to.html
If I understand you correctly, you don't want to display 'default.aspx' whenever someone comes into a folder with that document available.
So if they do hit it, you want to automatically redirect to the '/' and just load the default document anyway?
If that's the case then, as stated above, you run the risk of an infinite loop. The second comment gives you an answer but I guess expanding that to the re-write engine what you'd want is to:
Turn off default documents
Register each folder with the re-write engine
When that folder is requested load the default.aspx file as per your target rule
Does this sound about right?
I have to ask, why do you want to do this?
I'm not sure I understand what the problem is.
Though if you turn off default documents then / will simply point to the directory rather than the default.aspx page.
Leave default documents on and just do a redirect based on whether default.aspx is in the requested url or not.
well you can use regular .net to inspect httprequest url, if it has "default.aspx" in it, you can redirect to "/", there will be no infinite loop and you better do this on preload, and end response afterwards, to minimize time it takes to process

Resources