ISAPI redirects not working properly

Topics: Developer Forum
Dec 16, 2009 at 8:27 AM

Hi
We have a number of urls from our old site that we have redirected to our new site, however quite a few do not work for example:

1. RewriteRule qualifications/vocationally-relatedcertificate/clait/index.html$ /qualifications/type/vrq/clait/index.aspx  [L]
This redirects to the homepage as we already have a rule set up to redirect from /index.html to /index.aspx, so all other rules that have ‘index.html’ in them, redirect to the home page.

2. http://www.mydomain/it - This url is already set up in the ISAPI - But if I want to add this one - http://www.
mydomain/recruit, it goes to same place as the one above because it ends in ’it’.

3. RewriteRule qualifications/vocationally-relatedcertificate/text_processing_bus_pro_level_1_award/index.html$ /qualifications/type/qcf/text_pro/bp_l1_award/index.aspx  [L]
This redirects to the homepage but the following one is ok:
RewriteRule qualifications/vocationally-relatedcertificate/text_processing_bus_pro_level_1_award/documents.html$ /qualifications/type/qcf/text_pro/bp_l1_award/documents/index.aspx  [L]

I assume this is because at the root level there isn’t a /documents.html rule set up.
We have thousands of redirects in the .ini file, but there is an example below:

# Isapi.ini file output by Tridion
# See IW for syntax
 
RewriteLog  D:\ISAPIReWriteLog\iirfLog.out
RewriteLogLevel 0

# asp redirects
RewriteRule index.asp$ /index.aspx  [L]
RewriteRule 14-19$ /qualifications/14-19/index.aspx  [L]
RewriteRule 14-19alevel$ /qualifications/type/gce/  [L]
RewriteRule 14-19alevels$ /qualifications/type/gce/  [L]
RewriteRule 14-19changes$ /qualifications/14-19/index.aspx  [L]
RewriteRule 14-19diploma$ /qualifications/type/diploma/  [L]
RewriteRule 14-19diplomas$ /qualifications/type/diploma/  [L]
RewriteRule 14-19functionalskill$ /qualifications/type/fs/  [L]
RewriteRule 14-19functionalskills$ /qualifications/type/fs/  [L]
RewriteRule 14-19functionalskillspilot$ /qualifications/type/fs/pilot_reg/index.aspx  [L]
RewriteRule 14-19gcse$ /qualifications/type/gcse/  [L]
RewriteRule 14-19gcses$ /qualifications/type/gcse/  [L]
RewriteRule 14-19signup$ /qualifications/14-19/updates/index.aspx  [L]
RewriteRule 1419signup$ /qualifications/14-19/updates/index.aspx  [L]
RewriteRule 2010/$ /campaigns/2010/index.aspx  [L]


Many thanks

Coordinator
Dec 17, 2009 at 2:46 AM

Hi Paul, thanks for using the forums.

I took a look at your rules that you sent me offline, and I have some suggestions.  The first one will fix the three problems you described.  But there are other opportunities for improvement in that INI file.

  1. Every pattern (the first string that follows the word RewriteRule) should begin with a circumflex and a slash, unless there's a darn good reason not to.  The circumflex (^) indicates "beginning of line" and the slash is always present in URLs received by IIS.  The reason you want this is because , for example
         RewriteRule index.html$ /index.aspx  [L]
    will match an incoming URL like   http://server/qualifications/vocationally-relatedcertificate/clait/index.html , which you don't want.  You should change each rule, unless you have a good reason not to.  Example:
         RewriteRule ^/index.html$ /index.aspx  [L]
    This will fix the three problems you itemized - the two long qualifications URLs redirecting to the base homepage, and also /it and recruit/).
  2. All of the rules in the contactus section should use a / as the first character in the replacement pattern .
  3. There are numerous (80+?) rules like
         RewriteRule ^/news/2008/item_11.html$  /news/2008/item_11.aspx  [L]
    The numbers go from 01 to ... very high. You have one rule per number. You can use a wildcard here.
    The one rule to take care of all those URLs is:
         RewriteRule ^/news/2008/item_([0-9]+).html$  /news/2008/item_$1.aspx  [L]
  4. There are 84 rules for the old news from 2007, redirecting all URLs to the same target. They can all be replaced by one rule:
         RewriteRule ^/news/2007  /news/index.aspx  [L]
  5. Also, while the 2007 news rules are all Rewrites, I think you probably want Redirect here. Redirect is useful for any stale URLs that are no longer active, and judging from the context, it sure looks like that is true for "news" from 2007. Which means the one rule should really be:
         RedirectRule ^/news/2007  /news  [R=301]
  6. The same kind of wildcard approach is available to you with the section entitled "Past Papers Materials" .  There are 42 rules here, 38 of them should be replaced with a single rule.
  7. It sure seems like there is a good opportunity to use the RewriteCond and -f or -d patterns.  These test the existence of a file or directory in the filesystem, and I think using this you could probably eliminate 20% of your rules.  
  8. Another opportunity for you is the RewriteMap.  For example, each section could be handled by a map.

Good luck!

  

Dec 22, 2009 at 10:26 AM
Edited Dec 23, 2009 at 3:09 PM

Thanks for the reply, the three redirects are now working fine. We will have a look at the suggestions in 3. to 8. in the next few days. There is another problem which you might be able to help with.

We have the following neat url:
http://www.mydomain/switch
which goes to:
http://www.mydomain/administration/centre_app/general.aspx

We have a relative link on a button on all the pages with in this section, but because the ISAPI doesn't change the url in the address bar to the real url, the link takes you to:
http://www.mydomain/vocational.aspx which doesn’t exist, not to the proper link which is http://www.mydomain/administration/centre_app/vocational.aspx.
To get round this, we have had to add a redirect to get through to the proper page.

Is there any way the ISAPI rewrite can be configured so it changes the url in the address bar to the real url like a client side Response.redirect does?

Coordinator
Dec 22, 2009 at 3:33 PM

Nope!

You'll need to either

  • make your apps rewrite aware (not recommended)
  • introduce redirects or rewrites in the ini file to compensate

 

Dec 23, 2009 at 3:10 PM

OK thanks anyway.

Apr 21, 2011 at 10:43 AM
Edited May 25, 2011 at 10:46 AM

Hi

Could you give me some information on how I might use a wildcard in IIRF to redirect all requests for pages with an extension of .aspx to be redirected to extensions of .html?

The whole url is the same apart from the extension.

Coordinator
Apr 25, 2011 at 5:29 PM

Redirect?  or rewrite?

 

To redirect

RedirectRule  ^/(.+)\.aspx(\?|$)   ^/$1.html  [QSA,L]

To rewrite it is the same, only replace RedirectRule with RewriteRule. 

The breakdown:

  • ^ denotes "beginning of line" or in the case of URL matching "beginning or URL"
  • the .+ captures "a series of 1 or more characters".  The dot is a wildcard matching "any character" and the + is a quantifier meaning "one or more"
  • Then the \.aspx matches .aspx.  Because the dot is a wildcard it needs to be preceded by slash to match itself.
  • The next bit is a choice - it matches either \? or $.  The \? matches a question mark, again preceded by a slash because a question mark has a special meaning in regex.  We want to match an actual question mark, so it needs a backslash. Option b is $, another char with a special meaning. It denotes "end of line" or in this case "end of URL".  So the (\?|$) matches "either a question mark, or nothing further".   
  • The replacement string inserts the first capture group, which is everything that precedes .aspx on the original URL.  It then appends .html in place of .aspx. 
  • The rule is modified with a [QSA], which says "append any query string from the original URL". 
  • Also modified with an L, which says "this is the last rule for this request; don't evaluate any more rules".

Good luck.

May 3, 2011 at 10:13 AM
Edited May 25, 2011 at 10:48 AM

Hi

Many thanks for the rule, however I’ve tried what you supplied below, plus all different variations including using Rewrite and placing the rule at the top of the ini file, but it’s still not working.

I have tried it on our live site which should redirect .aspx to .html and also on our Staging site which needs to redirect .html to .aspx.

 

Many thanks

 


From: Cheeso [email removed]
Sent: 25 April 2011 18:30
Subject: Re: ISAPI redirects not working properly [IIRF:78312]

 

From: Cheeso

Redirect? or rewrite?

 

To redirect

RedirectRule ^/(.+)\.aspx(\?|$) ^/$1.html [QSA,L]

To rewrite it is the same, only replace RedirectRule with RewriteRule.

The breakdown:

  • ^ denotes "beginning of line" or in the case of URL matching "beginning or URL"
  • the .+ captures "a series of 1 or more characters". The dot is a wildcard matching "any character" and the + is a quantifier meaning "one or more"
  • Then the \.aspx matches .aspx. Because the dot is a wildcard it needs to be preceded by slash to match itself.
  • The next bit is a choice - it matches either \? or $. The \? matches a question mark, again preceded by a slash because a question mark has a special meaning in regex. We want to match an actual question mark, so it needs a backslash. Option b is $, another char with a special meaning. It denotes "end of line" or in this case "end of URL". So the (\?|$) matches "either a question mark, or nothing further".
  • The replacement string inserts the first capture group, which is everything that precedes .aspx on the original URL. It then appends .html in place of .aspx.
  • The rule is modified with a [QSA], which says "append any query string from the original URL".
  • Also modified with an L, which says "this is the last rule for this request; don't evaluate any more rules".

Good luck.

May 25, 2011 at 11:00 AM

Hi

Sorry to reply again but the rule you provided still does not change any .aspx request into a .html. Please let me know if you can help further as it's crucial that the server executes this for all pages as in the next few weeks we will be changing all of our live extensions back to .aspx.

Many thanks