Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

URL Rewriting with RegExp 1

Status
Not open for further replies.

bdichiara

Programmer
Oct 11, 2006
206
US
I have tried and tried to understand how this works, and I cannot figure out for the life me 1: Regular Expressions and 2:How to format them for URL-Rewriting.

I'm using some code I found online that does URL-rewriting but I don't know how to set up my rules. Here's an example:
Code:
<rule name="Search Page"><!--probably wrong...-->
	<url>/([a-zA-Z][\w-]{1,149})\/</url>
	<rewrite>/search.aspx?q=$1</rewrite>
</rule>
<rule name="Content Page">
	<url>/([a-zA-Z][\w-]{1,149})/([a-zA-Z][\w-]{1,149})\/</url>
	<rewrite>/content.aspx?Category=$1&amp;Item=$2</rewrite>
</rule>

Here are the rules I want.
1. I have a stylesheet and javascript files that I don't want to break when rewriting kicks in.
2. shows up as 3. converts to: 4. converts to:
Is this possible/difficult? Please help, I would greatly appreciate it.

_______________
_brian.
 
I know nothing at all about ASP.Net, but the following might point you in the right direction - I've tested it in WinForms and it seems to meet your requirements:

Code:
  Private Function MatchEval(ByVal m As Match) As String

    If m.ToString.StartsWith("Default") Then
      Return "(nothing)"
    Else
      If m.ToString.StartsWith("search") Then
        Return "s/"
      Else
        If m.ToString.StartsWith("content") Then
          Return "category/"
        Else
          Return m.ToString
        End If
      End If
    End If


  End Function

  Private Const dmn As String = "[URL unfurl="true"]http://www\.domain\.com/"[/URL]
  Private ptrn As String = _
   "(?<=" + dmn + ")" + "Default.aspx|search\.aspx\?q=|content\.aspx\?c=category&amp;p="

  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim rx As New Regex(ptrn)
    TextBox2.Text = rx.Replace(TextBox1.Text, AddressOf MatchEval)

  End Sub

with: [tt]Imports System.Text.RegularExpressions[/tt] at the top of the Form.


Hope this helps.

[vampire][bat]
 
You can find details of the regular expression language here:

[URL unfurl="true"]http://msdn2.microsoft.com/en-us/library/az24scfc(VS.71).aspx[/url]

To achieve what you want you will need to set your config file up like this:

Code:
<rule name="Default Page">
    <url>(?i)/Default.aspx</url>
    <rewrite>/</rewrite>
</rule>
<rule name="Search Page">
    <url>(?i)/s/(?<SearchText>.*)/</url>
    <rewrite>/search.aspx?q=${SearchText}</rewrite>
</rule>
<rule name="Categories">
    <url>(?i)/s/(?<Category>.*?)/(?<Page>.*?)/</url>
    <rewrite>/content.aspx?c=${Category}&amp;p=${Page}</rewrite>
</rule>

I'll explain the last one in steps:

<url>

(?i) - sets the RegEx to not be case sensitive
/s/ - matches anything that starts '/s/'
(?<Category> - starts a group that has a name 'Category' that can be used in the replace string.
. - matches any character
* - tells the RegEx to match the '.' zero or more times
? - tells the '.*' combination to not be greedy. So with '/hello/friend/' it will match 'hello' and stop there and will not go onto match 'hello/friend' leaving 'friend' for the following Page Group.
) - Closes the group
/ - matches '/'
(?<Page>.*?)/ - as with the Category group, matches everything up to the next slash and places it into a group called Page.

<rewrite>
/content.aspx?c=${Category}&amp;p=${Page}

This makes the new URL look exactly as written with ${Category} and ${Page} replaced with the text matched by the groups with the same name in the <url> rule.

The code you are using is likely to only rewrite URLs on the way into your web site. When your site writes out URLs it will need to re-write them correctly using some code similar to earthandfire's above. Rewriting URLs will cause any relative paths in your site to be messed up if you are rewriting a page to a different level in the directory structure. To prevent this you may want to investigate placing a <base></base> tag in the header of each page.

There are lots of niggling issues with re-writing URLs in Asp.Net for example the framework automatically generates the path for the main <form action=""> attribute. So make sure you do plenty of testing and ensure you know why you are doing this so you can decide what is and isn't going to be an issue.
 
Hey Aptitude,

Thanks for helping me out. I tried to use that XML and the system didn't like it. I guess it thinks the <SearchText> is an XML tag. I get:
Code:
 Exception Details: System.Xml.XmlException: The 'SearchText' start tag on line 8 does not match the end tag of 'url'. Line 8, position 35.

Any ideas?

_______________
_brian.
 
Ok, maybe this will help. I think I found a better one that uses RegEx, and it's it uses the web.config file. The setup is like this:

Code:
<add url="~/(.*)default\.aspx" mappedUrl="~/$1default.aspx" />
<add url="~/search\.aspx?q=(.*)" mappedUrl="~/search.aspx?q=$1" />
<add url="~/(.*)/(.*)\.aspx" mappedUrl="~/content.aspx?c=$1&amp;p=$2"/>

I decided just to use the query string variable for my search page, since I can't change the URL that my search form submits to. So I would like my rules as follows:
1 & 2 Remain the same
3. ~/search.aspx?q=search+text needs to stay the same.
4. ~/content.aspx?c=category&amp;p=page converts to: ~/category/page/

Now, I feel like I've tried everything to get #4 to work, but it keeps acting like it can't find the file, so the best I could do was get to look like: ~/category/page.aspx, however I still would like for that to work, if possible.

_______________
_brian.
 
Thanks for helping me out. I tried to use that XML and the system didn't like it. I guess it thinks the <SearchText> is an XML tag. I get:

Oops I didn't spot that issue. You can either replace < and > with &lt; and &gt; or use a CDATA section like this:

Code:
<rule name="Default Page">
    <url>(?i)/Default.aspx</url>
    <rewrite>/</rewrite>
</rule>
<rule name="Search Page">
    <url>![CDATA[(?i)/s/(?<SearchText>.*)/]]</url>
    <rewrite>/search.aspx?q=${SearchText}</rewrite>
</rule>
<rule name="Categories">
    <url>![CDATA[(?i)/s/(?<Category>.*?)/(?<Page>.*?)/]]</url>
    <rewrite>/content.aspx?c=${Category}&amp;p=${Page}</rewrite>
</rule>
 
Ok, here are my 2 URL Rewrites:
Code:
<add url="~/(.*)default\.aspx" mappedUrl="~/$1default.aspx" />
<add url="~/(.*)/(.*)\.aspx" mappedUrl="~/content.aspx?c=$1&amp;p=$2"/>
but I just need 1 thing: I want the URL to rewrite like this:

mydomain.com/category/
to
mydomain.com/content.aspx?c=category&amp;p=default

For some reason, anytime I try to setup a URL rewrite to something that doesn't end in aspx, it just acts if it's looking for a folder. I really would like my URLs to end in '/' rather than .aspx.

_______________
_brian.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top