Configuration :: URL Rewrite Module With Database Call?
Feb 25, 2011
Hello, I'm trying to implement URL rewriting for the first time. I must use URL Rewrite Module IIS7 per my hosting company. All of the examples I have found so far will rewrite a url such as:
www.domain.com/ShowProduct.aspx?ID=233&Title=Product-Title,
to: www.domain.com/ShowProduct/ProductTitle/233. The common scenario between these examples is that the rewrite chops off the .aspx & special characters and shuffles the query string parameters around. I get how that all works. However, I had something a little different in mind and I'm not sure if it can be accomplished.
My urls currently look like this: www.domain.com/ShowProduct?ID=233, and I would like them to look like this: www.domain.com/ShowProduct/Product-Title. The way I would imagine this working is, during the rewrite, pass the ID to the db and return the Title for the new url. You see, I currently don't need to pass the Title as a query string parameter and I'd prefer to not show the ID at all. This would be ideal. Can this be accomplished? Or
will I have to add the Title as a query string param and show the ID in rewritten url?
For example when a user enters www.homeforhire.com they should be redirected to default.aspx (for operational reasons the user is then redirected to www.homeforhire.com/home.aspx).
Instead an error is caught in the Rewrite module (which should not be activated). I am using an http module to perform URL rewriting.
It appears as if the default document setup is not working correctly, and the requested page ~/ is being sent to the Rewrite module, causing an error (which is then caught by my error handler and the user is redirected to the home page).
Entering www.homeforhire.com/default.aspx works correctly.
Can anyone tell me why the default document settings in the web.config do not appear to be working?
I'm probably going to use the URL rewrite module for IIS 7 eventually and I have a fairly straight forward question that I really can't find the answer to.
If you have a base case of:
http://yoururl.com/page.aspx?ID=7 You can obviously have it rewritten to: http://yoururl.com/page/7 or whatever you want.
My question is this: When using this module can you still use Request.Querystring["page"] on the rewritten querystring. How does the Request.URL stuff work. Does asp.net still provide the un-rewritten url or does it provide the rewritten one.
I would assume that your C#/asp.net code is completely unaffected by the url rewriting, as that's more or less the point, but I want to be crystal clear.
Secondary question: What is the best practice for how you should code a website when using the rewritten. Should you code links in the written style, or continue using querystrings?
The IIS URL Rewrite Module ships with 3 built-in functions:
* ToLower - returns the input string converted to lower case. * UrlEncode - returns the input string converted to URL-encoded format. This function can be used if the substitution URL in rewrite rule contains special characters (for example non-ASCII or URI-unsafe characters). * UrlDecode - decodes the URL-encoded input string. This function can be used to decode a condition input before matching it against a pattern.
The functions can be invoked by using the following syntax: {function_name:any_string}
The question is: can this list be extended by introducing a Replace function that's available for changing values within a rewrite rule action or condition?
Another way to frame the question: is there any way to do a global replace on a URL coming in using this module?
It seems that you're limited to using regular expressions and back-references to construct strings - i.e. there's no search/replace mechanism to replace every instance of X with Y in {REQUEST_URI}, without knowing how many instances there are.
I've had a quick glance at the extensibility introduced in the 2.0 RTW and don't see any 'light' means of introducing this.
I have a website hosted on IIS7 with the url rewrite module 2.0 installed. It is run by content management that looks at the url and returns a 401 error if the current user does not have permission to view the page. This gets picked up by the asp.net url authorization module which then kicks the page over to the loginUrl page as specified in the web.config (forms authentication).
This works perfectly on my local machine - which is IIS7 and windows7.
If the url is, say, /612/some-string the user gets directed to the login page at /66/login?ReturnUrl=/612/some-string
The url rewriting looks at the first part of the url for the document id. The real url would be this: index.aspx?documentId=612
Unfortunately, when I deployed this to our staging server, the ReturnUrl isn't the rewritten url, it's the original url. This causes all sorts of problems.
The staging server is also IIS7 with the url rewrite module 2.0 installed. It's Windows 2008 server SP2. Both are running asp.net 3.5.
My only guess is that the machine.config orders the default httpModules differently, and the .net forms authentication module is jumping in before the url has been rewritten.
I'll review that soon, but in the meantime has anyone experienced this problem and solved it?
Update
I also tried changing
Response.StatusCode = 401;
to
FormsAuthentication.RedirectToLoginPage();
Which gets me a bit ahead, but still directs the user back to the url that hasn't been rewritten.
I have web services built with ASP.NET and ASP.NET clients consuming them. When consuming the webservices, how would I to force the clients to use https?
I don't want to force the whole site to use https by turning on require SSL in IIS.
Can I use the IIS7 URL rewrite module to re-route http requests to https?
http://mysite.com -> https://www.mysite.com OK http://www.mysite.com -> https://www.mysite.com NOT WORKING
I guess the condition is not being satisfied when I enter www.mysite.com in the browser, so there's no redirect and the page serves as HTTP instead of HTTPS.
I think I just need to modify the condition pattern, but I have almost nothing regex knowledge and I need this asap.
Rewrite has been working like a charm when testing and using it locally. However when I uploaded my application, then it gave me some problems.Problem Overview
My site - http://www.site.com/
Site is stored in a subdirectory on my root hosting in a folder call "sub".
So when I go to the URL http://www.site.com/part1/file.aspx then in my Rewrite Rule the {URL} is given as "/sub/part1/file.aspx" instead of "/part1/file.aspx"
Problem/Question,Is there a way I can handle all the {URL} in my Rewrite Rules without including the "sub" in the {URL} and without having to change every single ones Pattern?
I can't for the life in me work out how to get the outbound rules to work. All my inbound are spot on (not included). I have dynamic and statis compression disabled, I moved the module up in the modules list just incase.The server is Windows 2008 R2 x64 if that makes a difference. Also not that the 'Content' pages get rewritten perfectly. I don't get it.
You can view the live site here: http://www.ink4u.co.uk
I am trying to make a rewrite rule to check whether the URLends with '.htm' or '.html', but does not contain 'Archive.aspx'. How can I do this with regular expression?
The url starts out like this: [URL]/test.htm(or .html), and ends up like this:[URL]
I am using a project that rewrites URL. I am not familiar with this code, but it works fine on IIS 6 and with VS2010. Problem begins when I need to deploy into IIS7.5 server. Seems like the rewrite doesn't work and the page doesn't fount after rewriting page.
[Code]....
I am trying to use this web.config code, but then I could see this:
See below code "ViewEntry.aspx" in web config file i am using to display post of my blog in the same way i want to use one more page to display categories of my blog.
How to use multiple url in web config file inside the rewriter tag?
I'm trying to implement URL Rewriting into my existing application and have managed to get the page and links working except that my destination page does not get the query string values.Mycde is based on the example below: http://dotnetguts.blogspot.com/2008/07/url-rewriting-with-urlrewriternet.htmlBasically I have a default.aspx page with links to another page; directory_item.aspx?Item_Id=1&Category_Id=1 directory_item.aspx?Item_Id=2&Category_Id=1 and so on... The code in my web config is as follows;
If application is started without debugging - it runs smoothly, when I press F5 I get: "Unable to start debugging on the web server. Could not start ASP.NET debugging. More information may be available by starting the project without debugging. Click Help for more information"I noticed that problems are caused by URL Rewrite section in web.config:
when I comment it out - I can start debugging. Also debugging works on VS's built-in web server.I'm running Win7 64 bit, VS 2010, application's framework is 4.0, in IIS application has ASP.NET 4.0 Intergrated pool set
i am making an application which is like chain system..i mean one person can have multiple child.. then his child can have multiple child ..etc like chain system...i dont know to insert into database.. so that when i call any-node.. his child,, and their futher child will show...
I am in the process of redesigning our main product, a knowledge database system for clients to access clinical and specialist advice articles. There is a requirement to add "Social Networking" to this allowing users to connect and post remarks etc...
This is somewhat similar to the Facebook wall scenario - where Articles, Photos, Groups, Products, Events, ActivityFeed can all have comments. I am at a lose as to the database design I would need to implement this. All comments must allow for Moderation/Approval/Spam Reporting etc...
I originally thought a individual table for each Comments entity ex: ArticleComment, ProductComment would work as the comments themselves are not all directly related. Each table having the same exact columns. But this makes life abit awkward as then this creates a separation between something that for all purposes is the same i.e. They are all comments.
The other design idea was to have a single Comment table with an "arc" type relationship to related tables with FK references back to the source.
The other option was to have a single Comment table with an Object Type and XID but this breaks the rules of relation and does not all for referential integrity.
I am at a lose and don't know what to do. I have asked over at SQLCentral.com and all I have done is raised more questions than answers about what to do.
Design 1: This would require exact copies of this table for all the entities.
-- Photo Comments -- CREATE TABLE dbo.PhotoComment ( CommentID int IDENTITY(1,1) NOT NULL, PhotoID int NOT NULL, Body ntext NOT NULL, ReportedAsSpam bit NOT NULL CONSTRAINT [DF_PhotoComment_ReportedAsSpam] DEFAULT (0), IsSpam bit NOT NULL CONSTRAINT [DF_PhotoComment_IsSpam] DEFAULT (0), IsApproved bit NOT NULL CONSTRAINT [DF_PhotoComment_IsApproved] DEFAULT (1), CreatedDate datetime NOT NULL, CreatedBy uniqueidentifier NOT NULL, UpdatedDate datetime NOT NULL, UpdatedBy uniqueidentifier NOT NULL, IsDeleted bit NOT NULL CONSTRAINT [DF_PhotoComment_IsDeleted] DEFAULT (0), CONSTRAINT [PK_PhotoComment] PRIMARY KEY (CommentID), CONSTRAINT [FK_PhotoComment_PhotoID] FOREIGN KEY (PhotoID) REFERENCES dbo.Photo(PhotoID), CONSTRAINT [FK_PhotoComment_CreatedBy] FOREIGN KEY (CreatedBy) REFERENCES dbo.aspnet_Users(UserId), CONSTRAINT [FK_PhotoComment_UpdatedBy] FOREIGN KEY (UpdatedBy) REFERENCES dbo.aspnet_Users(UserId) )
Design 2: The "Arc" relationship
CREATE TABLE dbo.Comment ( CommentID int IDENTITY(1,1) NOT NULL, CommentType char(1) NOT NULL CHECK(CommentType IN ('A','I','E','G','U','P')), Body ntext NOT NULL, ReportedAsSpam bit NOT NULL CONSTRAINT [DF_Comment_ReportedAsSpam] DEFAULT (0), IsSpam bit NOT NULL CONSTRAINT [DF_Comment_IsSpam] DEFAULT (0), IsApproved bit NOT NULL CONSTRAINT [DF_Comment_IsApproved] DEFAULT (1), CreatedDate datetime NOT NULL, CreatedBy uniqueidentifier NOT NULL, UpdatedDate datetime NOT NULL, UpdatedBy uniqueidentifier NOT NULL, IsDeleted bit NOT NULL CONSTRAINT [DF_Comment_IsDeleted] DEFAULT (0), CONSTRAINT [PK_Comment] PRIMARY KEY (CommentID), CONSTRAINT [FK_Comment_CreatedBy] FOREIGN KEY (CreatedBy) REFERENCES dbo.aspnet_Users(UserId), CONSTRAINT [FK_Comment_UpdatedBy] FOREIGN KEY (UpdatedBy) REFERENCES dbo.aspnet_Users(UserId), CONSTRAINT [UC_Comment_CommentID_CommentType] UNIQUE (CommentID, CommentType) )
CREATE TABLE dbo.Comment ( CommentID int IDENTITY(1,1) NOT NULL, ObjectType char(1) NOT NULL, /* A = Article, P = Photo etc... */ XID int NOT NULL, /* Would be the ID of the main entity being queried. */ Body ntext NOT NULL, ReportedAsSpam bit NOT NULL CONSTRAINT [DF_Comment_ReportedAsSpam] DEFAULT (0), IsSpam bit NOT NULL CONSTRAINT [DF_Comment_IsSpam] DEFAULT (0), IsApproved bit NOT NULL CONSTRAINT [DF_Comment_IsApproved] DEFAULT (1), CreatedDate datetime NOT NULL, CreatedBy uniqueidentifier NOT NULL, UpdatedDate datetime NOT NULL, UpdatedBy uniqueidentifier NOT NULL, IsDeleted bit NOT NULL CONSTRAINT [DF_Comment_IsDeleted] DEFAULT (0), CONSTRAINT [PK_Comment] PRIMARY KEY (CommentID), CONSTRAINT [FK_Comment_CreatedBy] FOREIGN KEY (CreatedBy) REFERENCES dbo.aspnet_Users(UserId), CONSTRAINT [FK_Comment_UpdatedBy] FOREIGN KEY (UpdatedBy) REFERENCES dbo.aspnet_Users(UserId), CONSTRAINT [UC_Comment_CommentID_ObjectType_XID] UNIQUE (CommentID, ObjectType, XID) ) -- Ex: Get all comments for article ID 23. SELECT * FROM Comment Where ObjectType ='A' and XID = '23'
I have several web sequential pages which will modify a record and its child records in the database, called a "project". Such a project is currently passed between pages using its database ID in the URL parameters.
A project has some information specific to itself, and also consists of one or more Tasks, which each have information specific to itself.
Is it faster (alternatively, more maintainable or more easily understood) to hit the database each time I need to query the same project (and its tasks), or should I query the database once (either once for each page or once for all pages and save to Session) and check the saved object rather than the database?