Rewrite: variable length query strings

Topics: Developer Forum, User Forum
Jun 16, 2008 at 5:34 PM

Hi

We have these possible URLs to map to these query strings:

URL: /web/france/

Mapped to: /content-web.aspx?country=france

URL: /web/france/paris/

Mapped to: /content-web.aspx?country=france&city=paris

URL: /web/france/paris/a-z/

Mapped to: /content-web.aspx?country=france&city=paris&area=a-z

We have this rule but i cannot get it to work:

RewriteRule ^/web/([^\.\/]+)/([^\.\/]+)/([^\.\/]+)$ /content-web.aspx?country=$1&city=$2&area=$3 [I]

I have searched the documentation with no luck.

Any help would be much appreciated.

Thanks

B

Coordinator
Jun 16, 2008 at 8:43 PM
Maybe try 3 separate rules?


# -------------------------------------------------------
# This set of rules shows how to transform a URL into a query string.
# There are three possibilities: the requested URL can have one, two or three segments.
# There is a distinct rule for each case.

# one segment
RewriteRule  /web/([^\/]+)/$  /content-web.aspx?country=$1    [L]

# two segments
RewriteRule  /web/([^\/]+)/([^\/]+)/$   /content-web.aspx?country=$1&city=$2  [L]

# three segments
RewriteRule  /web/([^\/]+)/([^\/]+)/([^\/]+)/$  /content-web.aspx?country=$1&city=$2&area=$3    [L]

#add a trailing slash when there is no query string (use a negative lookbehind)
RewriteRule  /web/(.+)(?<!\/)$    /web/$1/ 

 

 

Jun 17, 2008 at 8:10 AM

Hi Cheeso

Thank you for your post.

Thats great, i forgot to mention:

The page may also not have any querystring so have modified the last rule to:

RewriteRule  /web/   /content-web.aspx/

My only concern is how to deal with the issue where the trailing slash (/) is missing so if someone types: www.domainzzzz.com/web or www.domainzzzzz.com/web/france

Thanks
B


Coordinator
Jun 17, 2008 at 9:51 AM
     My only concern is how to deal with the issue where the trailing slash (/) is missing so 
     if someone types: www.domainzzzz.com/web or www.domainzzzzz.com/web/france 

That's what the final rule was for. The rule you changed.

Consider adding the simple rule you created - put it at the very beginning of the list - and keeping the final rule I proposed.

Jun 17, 2008 at 10:26 AM
Edited Jun 17, 2008 at 10:27 AM
Hi

Thanks for your post.

With the last rule in place the query string does not appear to be passed.  If i remove it the query string works unless the final slash is removed.

Thanks
B
Coordinator
Jun 17, 2008 at 9:50 PM

With the last rule in place the query string does not appear to be passed. If i remove it the query string works unless the final slash is removed.

Sorry, I'm not understanding what you meant by that. What results do you see? What results do you expect?

Jun 18, 2008 at 7:44 AM

Sorry my reply was not good....

With the rules in place as you suggested, plus the additional first rule:

# no querystring
RewriteRule  /web/   /content-web.aspx/ [I]

# one segment
RewriteRule  /web/([^\/]+)/$  /content-web.aspx?country=$1    [I,L]

# two segments
RewriteRule  /web/([^\/]+)/([^\/]+)/$   /content-web.aspx?country=$1&city=$2  [I,L]

# three segments
RewriteRule  /web/([^\/]+)/([^\/]+)/([^\/]+)/$  /content-web.aspx?country=$1&city=$2&area=$3    [I,L]

#add a trailing slash when there is no query string (use a negative lookbehind)
RewriteRule  /web/(.+)(?<!\/)$    /web/$1/ [I]

This works perfectly: www.domainzzzzz.com/web/france/  but this does not: www.domainzzzzz.com/web/france  it returns the page correctly but the query string is not in place.

Also: This does not work at all: www.domainzzzzz.com/web returning The page cannot be found

Sorry for my last post.

Thanks B

Coordinator
Jun 18, 2008 at 3:13 PM

Bernie,
I'm not really folllowing you.

you wrote: 

This works perfectly: www.domainzzzzz.com/web/france/  but this does not: www.domainzzzzz.com/web/france  it returns the page correctly but the query string is not in place.

Also: This does not work at all: www.domainzzzzz.com/web returning The page cannot be found

The thing is, IIRF isn't responsible for finding your page. When you get "page not found" - that is not IIRF's concern. IIRF just rewrites the incoming URL to something else. It is up to YOU to ensure that the something else exists. Here's what I mean. You can have a RewriteRule that says "rewrite everything to "/Foo/ThisPageDoesNotExist.htm" . And maybe that page doesn't exist. IIRF can function perfectly, and the user will see 404 Page Not Found errors. This is because you've rewritten the incoming URLs to something that, really, truly, does not exist.  If you, as website admin, want to use URL rewriting, then YOU are responsible for (1) rewriting incoming URLs the way you want to, and (2) insuring that the rewritten URLs actually exist.   I can help you with #1  (IIRF stuff and RewriteRules).  You are on your own for #2. 

The second thing, you said "it returns the page correctly but the query string is not in place.". I don't know what this means. The query string is not in place where? What do you see? What do you expect to see? If you are thinking that the query string will change in the browser address bar, that is not what happens with a URL Rewrite. Check the readme again, and consult the references on URL Rewriting that I offered on the home page of this Codeplex project. If you want the URL to change in the browser address bar so that the user sees he is surfing to a different URL, then you need to use a redirect. IIRF can help you with that too.  Read up on it. (If you are NOT thinking that the URL would change in the browser, then I don't know what you mean by "the query string is not in place.". )

Jun 18, 2008 at 6:24 PM
Hi

Thank you for your post.

I will re-phrase my question:

If my user types in www.domainzzzzz.com/web or www.domainzzzzz.com/web/france can I detect a missing slash on the end and add it before running any more rules?

Thanks
B
Coordinator
Jun 18, 2008 at 9:47 PM

Hi Bernie,

this rule (which I suggested originally) is designed to do that.

#add a trailing slash when there is no query string (use a negative lookbehind)
RewriteRule  /web/(.+)(?<!\/)$    /web/$1/ 

You've said that it didn't work, and so you removed it. 

I'm not clear why you removed it, or what results you were seeing that you did not like.
When you say you want to "add" the slash - do you want to ADD it to the URL Address that is visible in the browser?
if so, you need a REDIRECT.

Jun 19, 2008 at 8:00 AM
Hi

Thanks for your post.

I removed it initially but added it back in as advised in your post dated Tue at 10:51 AM.

We now have exactly what I posted on Wed at 8:44 AM (with the rule you mention), my only modification was to add the [I] to make it case in-senstive, would this have caused it to stop working?

The slash is not necessary for a visual point of view only to achieve desired affect of passing the query string.

Kind regards
B
Coordinator
Jun 19, 2008 at 4:43 PM
Ok... so.... where are we?  Is there something else?   Are you set?