Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

Match part of a URL only if it is not in a subfolder

Last post 07-04-2008, 12:24 PM by liquidM. 6 replies.
Sort Posts: Previous Next
  •  07-03-2008, 12:04 PM 43728

    Match part of a URL only if it is not in a subfolder

    Hi,

    I am using regular expressions in a .net environment to do URL rewriting. The rewriting engine is only passed the part of the URL after the domain name, so for example the URL:

    www.domain.com/pagename.aspx

    would pass the value "/pagename.aspx" to the rewriting engine.

    The issue comes from the fact that I want the URL rewriting engine to leave anything in a subfolder alone, so

    www.domain.com/subfolder/pagename.aspx

    which passes the value "/subfolder/pagename.aspx" should not be matched.

    The regular expression I have at the moment is:

    /(.*)(.aspx)$

    which matches both the examples above. I guess I just want to say something along the lines of "if there is an additional slash after the first one then don't match". This should be quite an easy one, but I just can't grasp hold of regular expressions for some reason!

    Thanks,
    Laura

     

     

     

     

  •  07-03-2008, 12:35 PM 43729 in reply to 43728

    Re: Match part of a URL only if it is not in a subfolder

    ^(/)((?!\1).)*$
  •  07-04-2008, 4:35 AM 43747 in reply to 43729

    Re: Match part of a URL only if it is not in a subfolder

    That's wonderful, thanks for your time and expertise!

     Laura

  •  07-04-2008, 5:06 AM 43749 in reply to 43747

    Re: Match part of a URL only if it is not in a subfolder

    Hmm... looks like I just need to take this one step further... I simplified the problem to make it easier to get a solution on here, but it is slightly more complex!

    Because we are using URL rewriting, we are actually using the regular expression to replace the existing URL with the new one. I have modified the regular expression you gave me accordingly and for "normal" URLs, it works fine. The problem is when you get querystrings involved...

    Our aim is to convert the string:

    /mypage.aspx?param1=value1&param2=value2

    to:

    /default.aspx?pagename=mypage&param1=value1&param2=value2

    I.e. whatever page we come in on actually redirects to default.aspx followed by ?pagename= followed by the page name, followed by the original querystring.

    The current regular expression I have, based on what I've been given on here, is:

    ^(/)(((?!\1).)*)(.aspx)(^.*)*.

    which is replaced by

    /default.aspx?pagename=$2&$3

    This is almost correct, but unfortunately it seems the last letter in the pagename is finding its way into $3, e.g.

    /mypage.aspx?param1=value1&param2=value2

    goes to 

    /default.aspx?pagename=mypage&eparam1=value1&param2=value2

    Any help again much appreciated, as I'm almost there now!

    Laura

  •  07-04-2008, 9:36 AM 43758 in reply to 43749

    Re: Match part of a URL only if it is not in a subfolder

    If you need to have the ordering as shown in your last post then it would seem you would need two patterns:

    Raw Match Pattern:
    ^/([^/]*)\.aspx\?

    Raw Replace Pattern:
    /default.aspx?pagename=\1&

    Raw Match Pattern:
    ^/([^/]*)\.aspx$

    Raw Replace Pattern:
    /default.aspx?pagename=\1

    If you don't mind if the query string portion of the URL is reordered (as a browser certainly wouldn't care) you can do it with one pattern:

    Raw Match Pattern:
    ^/([^/]*)\.aspx(.*)

    Raw Replace Pattern:
    /default.aspx\2&pagename=\1


  •  07-04-2008, 12:15 PM 43763 in reply to 43749

    Re: Match part of a URL only if it is not in a subfolder

    liquidM:

    Hmm... looks like I just need to take this one step further... I simplified the problem to make it easier to get a solution on here, but it is slightly more complex!

    We explicitly ask in the posting guidelines that you do not simply your problems for that very reason. You will get a solution to what asked for and not what you need. In the future ask for what you really need, it will save time.


    Michael

    "In theory, theory and practice are the same. In practice, they are not."
    Albert Einstein
  •  07-04-2008, 12:24 PM 43764 in reply to 43758

    Re: Match part of a URL only if it is not in a subfolder

    That's great, thanks again! I do have to have them in a certain order, so had to use the two seperate statements.

    I seemed to have to make a slight change to one of them, changing

    /default.aspx?pagename=\1&

    to

    /default.aspx?pagename=$1&

    as it didn't appear to work with the \ for some reason? Anyway, cheers, if you ever come to England I will buy you a drink! Smile

    Laura

View as RSS news feed in XML