Rewrite Rule Help

Topics: User Forum
Apr 15, 2010 at 12:09 AM

Hello, I hate having to ask but I need help with my rewrite rule. 

I have this in my IIRF.ini

RewriteRule ^((/([^/.\?]+))*)/*$ /templates/?headline=$1

The url is www.domain.com/man-on-moon which send the headline variable as /manonmoon

Why did it remove the hypens and how do i get rid of the backslash.

If only I knew what I was doing it would be simple....

Many Thanks.

Rich

Apr 15, 2010 at 5:29 AM

Why so many groups if you only need the portion after the last backslash?  Plus I'm not sure of the logic, the first group would seem to capture everything but any slashes at the end, the second group captures the first slash plus any 1 or more characters that are not "/", ".", or "?", and the 3rd group gets the same as the 2nd with no leading slash.  I have no idea why you're losing the hyphens, I can't see where that would be happening. So /man-on-moon gets you $1="/man-on-moon", $2="/man-on-moon", $3="man-on-moon"

Wouldn't it be simpler to do

RewriteRule /(.*)$ /templates/?headline=$1 [L]

However, (trying to refine my regex skills a bit) if you don't want to quit rule processing at that point, but don't want to screw up your URL after it re-iterates, you could use

RewriteRule /([^=]*)$ /templates/?headline=$1

The first rule grabs everything after the last "/" stores it in $1, rewrites the pattern, than quits.

The second rule grabs everything after the last "/"*as long as there is no "=" sign (you could run into problems if any of your headlines have "=" in them), then acts as rule #1, otherwise it skips it.

I'd also suggest that your substitution pattern doesn't look right, most people want something like /templates.aspx?headline=man-on-moon , not /templates/?headline=man-on-moon.

Apr 15, 2010 at 6:17 PM

 

The big problem is I'm really having difficulties understanding the regular expressions. 
The url's need to work like this.
www.domian.com/man-on-moon   /templetes/?headline=$1
www.domian.com/templetes/?headline=man-on-moon     / no rewrite
www.domian.com/templetes/?id=1    / no rewrite
I'm not sure why the developer used the path with out the file name. templetes/?headline=man-on-moon is the same as templete/default.aspx?headline=man-on-moon. I was just trying to stick with the previous developers code patterns. 
I don't know where I found the RewriteRule ^((/([^/.\?]+))*)/*$ or how to interpret what its doing but it appears to work fine except the removal of - and the begin / .  This could be dangerous.

The big problem is I'm really having difficulties understanding the regular expressions and the concept of IIRF.

I tried you rule and it worked but I found issues with the images and other url's. This got me to thinking. Do my url's need to be formated like this

www.domain.com/articles/man-on-moon  then write a rule that only affects the articles directory? So www.domain.com/anythingElseWantBeAffected.aspx

The url's need to work like this.

www.domian.com/man-on-moon   /templetes/?headline=$1
www.domian.com/templetes/?headline=man-on-moon     / no rewrite
www.domian.com/templetes/?id=1    / no rewrite
www.domian.com/default.aspx   / no rewrite
www.domain.com/ContactUs  / no rewrite 

I'm not sure why the developer used the path with-out the file name. templetes/?headline=man-on-moon is the same as templete/default.aspx?headline=man-on-moon. I was just trying to stick with the previous developers code patterns. 

I'm so ready to give up but I've made it this far.... 

Thanks for your reply's

 

Coordinator
Apr 16, 2010 at 2:14 AM
Edited Apr 16, 2010 at 2:14 AM

There are some really good examples in the documentation that cover these scenarios.

Definitely check it out.   The - replacement pattern is the one you want to use for "no rewrite."

good luck!

Apr 16, 2010 at 2:57 AM

 

A good book on Regular expressions would be a good thing to get.  O'Reilly's books are usually among the best, you should look into this:   Regular Expression Pocket Reference, Second Edition

The concept of IIRF is simple but powerful, and is essentially equivalent to mod_rewrite on Apache (if you have a Linux or LAMP background you'd probably be familiar with that).  Basically it's a ISAPI application that runs in same process space (that whole concept varies a bit murky as you look at different versions of IIS) as IIS, but provides additional functions.  IIRF provides the ability to reconfigure the incoming URL before your web site or web application sees it.

So it works like this:

  1. User types (or clicks a link) a request to your webserver.
  2. Your webserver receives the incoming request
  3. The webserver hands the request off to the IIRF ISAPI filter
  4. IIRF modifies the incoming requested URL according to whatever rules you set
  5. IIRF hands the resulting URL back to the webserver
  6. The webserver processes the URL request and sends the result back to the user

So the webserver never processes the original URL, it process the resulting URL from IIRF.  This is very powerful, as it allows you to essentially control what the webserver processes, and where the requests end up.

Let's think about your requests.  First, keep in mind that IIRF doesn't ordinarily mess with the domain name, just the path, so the URL it processes is /default.aspx, not www.domain.com/default.aspx. Then you should order them by highest priority rules first (I'll correct the obvious typos, you should be careful about those when asking about syntax, it can make a lot of difference):

  1. www.domain.com/default.aspx   / no rewrite
  2. www.domain.com/ContactUs  / no rewrite
  3. www.domain.com/templates/?headline=man-on-moon     / no rewrite
  4. www.domain.com/templates/?id=1    / no rewrite
  5. www.domain.com/man-on-moon   /templates/?headline=$1

That seems to me the easiest order to deal with your rules, get rid of the do nothing rules first, than move to the action.

 

You can pretty much roll the first 4 into one rule, assuming that by rules 3 and 4 you basically mean to ignore anything with the pattern "/templates/?".  I'm also assuming you either won't encounter or don't care if there's anything after "/default.aspx" or "/ContactUs"  Assuming those things, it's pretty straightforward:

RewriteRule ^/(default.aspx|ContactUs|templates/\?) - [I,L]

Basically this looks for a leading "/" followed by "default.aspx" or "ContactUs" or "templates/?" (you have to escape the "?" with a "\" because it's a special character).  If there's a match, the "-" tells IIRF to do no substitutions.  The "I" modifier tells IIRF to ignore case (I'm assuming you want to do the same thing if the URL is "/ContactUs" versus "/contactus" or even "/CoNtAcTuS"), and the "L" modifier tells IIRF to quit processing and skip the rest of the rules if there's a match.

The last rule is only one with any substitution and it's pretty much what we looked at before.  If you place it after the above rule, you know that the other cases are already ruled out (because the "L" flag would've told IIRF to quit).  So you can safely use:

RewriteRule /([^=]*)$ /templates/?headline=$1

or even simpler, as we know that we don't have to worry about checking for the "=" now that the first rule will already quit with the "/templates/?" pattern (remember IIRF will rerun the rules after every match up to it's iteration limit unless you tell it to quit with the "L" flag):

RewriteRule /(.*)$ /templates/?headline=$1 

Done!

Cheeso, I hope you don't mind me jumping in on these, I'm trying to improve my Regex skills, and I've found that the best way for me to learn something is to try to explain it to someone else ;-).


Coordinator
Apr 16, 2010 at 3:08 AM

Randy, I don't mnind at all, it's much appreciated.

 

Apr 16, 2010 at 3:46 AM

Great, and please feel free to correct any goofs I make!