Regexp to make SEO-friendly URL´s

Discussion in 'C#' started by filt, Mar 3, 2007.

  1. #1
    I have a script to make dynamic links in articles on my website.


    <%
    
    my_text = "I like [[1,dogs]] and [[2,cats]]"
    
    Set RegularExpressionObject = New RegExp
    
    With RegularExpressionObject
    .Pattern = "\[\[(.*?),(.*?)\]\]"
    .IgnoreCase = True
    .Global = True
    End With
    
    new_text = RegularExpressionObject.Replace(my_text, "<a href=/main.asp?article_id$1>$2</a>")
    
    Response.Write "Replaced " & my_text & "<br>With: " & new_text
    
    %>
    Code (markup):
    Now i wish to add SEO-friendly URL´s. I have a database called articles with the following tables:

    article_ID, seo_url and article_text

    I want to add the following query to the code above

    set rs = connect.execute("SELECT * FROM articles WHERE article_ID = "&article_id
    article_url = rs("seo_url")
    Code (markup):
    I have an article with article_ID = 1, seo_url = information_about_cats

    So if i write [[1,dogs]] the URL should not be /main.asp?article_id=1 but it should be /information_about_cats as it is in the database.

    Id be really happy if anyone knew how to do this...
     
    filt, Mar 3, 2007 IP
  2. filt

    filt Member

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #2
    Just to make it clear, this has nothing to do with htaccess or rewriting urls, its just that i want it to be easy to crosslink diferent articles.

    If there is an article about bananas wich has ID 3, and im writing an article on fruitsalad, i just want to be able to link to the bananas-article by typing

    In a fruitsalad you might want [[3,bananas]].
     
    filt, Mar 3, 2007 IP
  3. filt

    filt Member

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #3
    I managed to get it working in PHP, now how do i translate this to ASP?

    <?php
    $text = 'This is a sample text, [[18,Headline]]. Thats it';
    $text = preg_replace('#\[\[(.*?),(.*?)\]\]#es',
                   'foo("\\1", "\\2")',
                   $text);
    echo $text;
    
    function foo($id, $subject)
    {
           /** Do a query based on $id */
    
           $url = 'min_rubrik.html'; /** the result from the database */
    
           return sprintf('<a href="%s">%s</a>', $url, $subject);
    }
    ?>
    PHP:
     
    filt, Mar 3, 2007 IP