coldfusion generate rewritemap for web.config - coldfusion

I have a rewritemap in my web.config that rewrites urls.
<rewriteMap name="Products">
<add key="Ipad_3" value="4399" />
<add key="Ipad_Mini" value="4399" />
</rewriteMap>
Instead of manually inserting new <add> tags, I would like coldfusion script to generate <add> tags with data taken from database and insert it into web.config. Please advice

That isn't going to work the way that you probably want it to.
Changes to web.config aren't recognized until your IIS application pool is recycled. Recycling your application pool every time you add or delete a product seems like a bad idea. Not to mention the fact that if anything goes wrong, your entire website will go down because of a bad web.config file.
You should look into using a dynamic rewrite tool like ISAPI Rewrite instead.

Related

IIS redirect rule not catching specified RegEx

I am trying to configure a Redirect Rule for our IIS server.
I have examined all the documentation as well as several other examples on the site, however I cannot seem to get the requested URL to capture when the pattern matches. I tried testing the regex inside the IIS rule editor and it shows that it matches correctly. I also set up some conditions and tested them however, it seems to not work either.
here is the rule in xml
<rule name="product_detail.asp redirect to aspx" stopProcessing="true">
<match url="http:\/\/ten\.perle\.com\/products\/product_detail.asp\?(.*)&(.*)&(.*)" />
<conditions>
<add input="{QUERY_STRING}" pattern="http:\/\/ten\.perle\.com\/products\/product_detail.asp\?(.*)&(.*)&(.*)" />
</conditions>
<action type="Redirect" url="products/product_detail.aspx?{C:1}&{C:2}&{C:3}" appendQueryString="false" />
</rule>
sample url http://ten.perle.com/products/product_detail.asp?a=2&i=08000514&c=1650
I don't understand what exactly I am doing wrong It appears as though my regex should catch the request but for some reason it never seems to.
I feel like there seems to be some problem with your URL rule.
For example, Send a request to the following URL:
http://ten.perle.com/products/product_detail.asp?id=123&name=test
The Pattern in the Match URL should be the path part of the URL:
products/product_detail.asp
The QUERY_STRING server variable matches the part of the URL's
parameter string: id=123&name=test
You can try to modify your rule and see if it works.
In addition, You can use Failed Request Tracing in IIS to troubleshoot. The log file in the FailedReqLogFiles folder can see the whole process of rewriting in detail, and can check where the match fails.
So the Issue was solved using the below rule
<rule name="product_detail.asp redirect" stopProcessing="true">
<match url="^(products/product_detail.asp?)([^x]+)$" />
<action type="Redirect" url="products/product_detail.aspx?{R:2}" />
</rule>
Some things That I would like to include in case anyone else faces a similar issue. Some things to clarify.
the match URL portion should include the relative path of the URL you're going to be redirecting. In my initial attempts I was unsure if to include a full path or just the relative portion. This isn't fully clear in any of the documentation, at least its not explicitly stated anywhere i could find.
The redirect URL needs to contain everything required after the base URL for the new URL. Including parameters if required either pulled via rules {R:x} or from the conditions {C:x}
It took me some time to understand the parameter capturing, this is based on the regEx, if you test it it will show you how the url match is broken up into numbered rules based on your regEx
I ended up not need the query string condition for my use case, it was simpler for me to use the rules capture from the initial pattern.
also btw was working on IIS v10.0
Hope this helps some other beginners looking to do this and clarify some more of the less obvious aspects of IIS redirects.

IIS rewrites and handlers conflict

I'm trying to set up a Django/Angular application under IIS.
When I set a similar application under nginx, I pass all URLs starting with /api (my backend) or /admin (the Django admin interface) to Django. I also set two locations: / and /static - both are aliases to the folder with all the static resources.
I need to alias /static, too, because Django's admin application references resources at /static/admin/...
I can't get IIS to work the same way. I'm using wfastcgi to interface with Django, and a rewrite rule to map /static back to /. This is the relevant portion of my Web.config:
<rewrite>
<rules>
<rule name="Static Perfix" stopProcessing="true" >
<match url="^static/(.+)" />
<action type="Rewrite" url="{R:1}" />
</rule>
</rules>
</rewrite>
<handlers>
<add name="Admin"
path="/admin"
verb="*"
modules="FastCgiModule"
scriptProcessor="..."
resourceType="Unspecified" />
</handlers>
This doesn't work. When I access /admin the handler catches it and forwards the request to Django, as it should. Django return an html page with resources located at /static/admin/base.css (for example).
When the browser tries to load such a resource the rewrite rule catches it, rewrites it to /admin/base.css, and then the handler catches it and forwards it to Django, which doesn't know what /admin/base.css is and returns a 404.
I tried making /static a Virtual Directory pointing to the same physical directory as the root directory. This caused all sorts of conflicts because my root Web.config was read twice, causing all sorts of duplicate key violations (for all the keys I have defined, more or less).
I would appreciate any help on getting out of this situation.
I ended up avoiding the problem altogether, by restructuring my static resources folder and adding an actual static subfolder that contains the Admin static files.
I will no longer be able to use manage.py collectstatic for front-end deployment, though, I will need to create an additional script. Oh well.

Why my httpsessionstate is loosing value?

I'm dealing with the Sitecore app that stores some 'items' into the cart for users that are not authenticated (I guess they should not be) using code like this:
this.Session["abc"]
Then once user arrive on cart page and wants to proceed than if he waits too long more than a minute than value in the session is lost !
Here is my web.config relevant settings:
I'm at the end of my ropes here and don't know what is cleaning my session?
<sessionState mode="InProc" cookieless="false" timeout="20" sessionIDManagerType="Sitecore.FXM.SessionManagement.ConditionalSessionIdManager">
<providers>
<add name="mongo" type="Sitecore.SessionProvider.MongoDB.MongoSessionStateProvider, Sitecore.SessionProvider.MongoDB" sessionType="Standard" connectionStringName="session" pollingInterval="2" compression="true" />
<add name="mssql" type="Sitecore.SessionProvider.Sql.SqlSessionStateProvider, Sitecore.SessionProvider.Sql" sessionType="Standard" connectionStringName="session" pollingInterval="2" compression="true" />
</providers>
</sessionState>
and
<authentication mode="None">
<forms name=".ASPXAUTH" cookieless="UseCookies" timeout="90" />
</authentication>
I guess you don't need to be authenticated to use the session?
And yes I did check the code ensuring that nothing nullifying my session.
Issue with 1 minute session timeout often happens when you don't have VisitorIdentification in your layout. Sitecore considers every new visitor as a potential crawling robot and tries not to use 20 minutes session if not necessary.
Make sure you have VisitorIdentification in your layouts. You can add it for Web Forms like that:
<%# Import Namespace="Sitecore.Analytics" %>
<sc:VisitorIdentification runat="server" />
and for MVC like that:
#using Sitecore.Mvc.Analytics.Extensions
#Html.Sitecore().VisitorIdentification()
If your Visual Studio still complains that it cannot find VisitorIdentification type in Sitecore.Web.UI.WebControls namespace, check whether Sitecore.Analytics is referenced from your web project and if Sitecore.Analytics is registered in <system.web><pages><controls> in web.config:
<add tagPrefix="sc" namespace="Sitecore.Web.UI.WebControls" assembly="Sitecore.Analytics" />
EDIT:
Think about having thousands of sessions for robots and each of them can be pretty heavy in terms of RAM usage. If they are all kept for 20 minutes, they can kill the server easily. That's why Sitecore changes session timeout for every new user and sets it to 1 minute.
If Sitecore layout is configured properly and there is VisitorIdentification included, your browser will automatically execute another request to your server and your session timeout will be extended to default setting from web.config (e.g. 20 minutes)

Microsoft IIS URL Rewrite - regex

Im currently working with my IIS Server and ive installed the IIS rewrite module.
I currently have a rule on it which allows for all my coldfusion templates to be parsed really easily.
it generates the following pattern
^([^/]+)/?$
which covers any request for any template on the following page
index.cfm?action={R:1}
This rule simply parses any request into my website and makes it go directly to my index.cfm page.
What Im trying to understand is how to get the following.
^/css/ - then any script or sub folder to process like any other page request
^/js/ - then process any js file as it is stored on my server
^/assets/ - process any file in this folder as a normal file.
Ive never used this module before but im struggling to find out how to treat normal files like js and css as they are and in their respective directories.
I have no problem in writing the rules so each is prefixed with the folder names but cant for the life of me work out where to get any information on this from.
As long as you specify a rule before the one that catches everything, you should be able to use the stopProcessing attribute to prevent the module from doing anything with the request. There's no need to specify an action. For example:
<rule name="CSS files" stopProcessing="true">
<match url="^/css/" />
</rule>
Here is for all static files
<rule name="Static Files" stopProcessing="true">
<match url="^(.*)$" />
<conditions logicalGrouping="MatchAll">
<add input="{SCRIPT_NAME}" pattern="\.(bmp|gif|jpe?g|png|css|js|txt|pdf|doc|xls)$" ignoreCase="true" />
</conditions>
<action type="None" />
</rule>
Update:
I am sorry, I haven't notice you have asked for folder. But I keep in answer in case other people like to know about it.

Is this a valid regular expression in IIS 7?

Further to my question at How do write a rewrite rule in IIS that allows HTML anchors? I was wondering if I can do the following or alternatively if there is a similar option to what is available in Apache e.g. mod_rewrite with anchor link
<match url="^ts\/tcs\.aspx$" />
<action type="Redirect" url="http://www.abc.com" />
<conditions>
<add input="{UrlEncode:{URL}}" pattern="#" />
</conditions>
Here we go:
Everything after the hash (including the hash symbol itself) will NOT be sent to a server (it is client-side ONLY), therefore you cannot match it in any way.
It is not IIS restriction -- any web server does (better say, should do) the same -- the browser itself should not send it to a server in first place. Of course -- if you send such request manually via custom code (PHP, ASP.NET etc) and corresponding function/class/component does not strictly follow standards, then it may get sent and then you will have it as part of query string (but the web server may reject such request, although unlikely).
If URL is: /something.php?task=show#kitten, then #kitten is the hash (including the actual hash symbol #). The proper name of kitten here is Fragment Identifier.