SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Zealot
    Join Date
    Jan 2006
    Posts
    169
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need help with rewrite problem

    I am using an seo url contribution on my oscommerce store. When I upgraded to Mysql 5, one of my queries broke and it resulted in the URLs being written as:

    mysite.com/-c-145.html

    instead of

    mysite.com/my-category-c-145.html

    Now I've fixed the query, but I want to redirect anybody going to the former url to my url rewriting function (see below). I already am using the following redirect code:

    Code:
    RewriteRule ^(.*)-c-(.*).html$ index.php?cPath=$2&%{QUERY_STRING}
    which rewrites the old style urls to the new style. I need to figure out how to modify that rewrite rule or add another one that also redirects the request if the "my-category" portion is missing and it starts with "-c-".

    Thanks! I am regex challenged, so please be easy on me.

  2. #2
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,607
    Mentioned
    19 Post(s)
    Tagged
    2 Thread(s)
    wm,

    I agree.

    The first thing to do is to get rid of the (.*) as it's more trouble than it's worth. It appears that the first one should be replaced with ([-a-z]+) and the second with ([0-9]+).

    Since your problem has arisen from not having a cPath value, let me assume that NO cPath value should be assigned my-category and say that you should preceed your RewriteRule with another which specifically tests for a null value in the URI (your /-c-145.html) and redirect that WITH the my-category value for cPath.

    Second, adding {QUERY_STRING} is a poor excuse for not using the QSA flag which automatically appends a pre-existing query string.

    Of course, had you taken the time to read the tutorial Article linked in my signature, you'd know all that already!

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  3. #3
    SitePoint Zealot
    Join Date
    Jan 2006
    Posts
    169
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the guidance. I've quickly browsed through your article. It's very informative. Unfortunately, being regex challenged (aka a complete regtard), I now know what I need to do, but seem to be having trouble actually doing it.

    What is the regex string that tests for -c- immediately after the / ?

  4. #4
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,607
    Mentioned
    19 Post(s)
    Tagged
    2 Thread(s)
    wm,

    It depends upon whether you're on Apache 1.x or 2.x (leading / treatment is different) but the following will work for the "one off" -c- => my-category-c- RewriteRule for either flavor:
    Code:
    RewriteRule ^/?-c-([0-9]+)\.html$ index.php?cPath=my-category [QSA,L]
    The / is required for Apache 1 and assumed by Apache 2 so the optional leading slash ( /? ) will work for both. The QSA flag will append the pre-existing query string and the Last flag terminates that RewriteRule (so it's not automatically ANDed with the next mod_rewrite block).

    Sorry, I'd thought that the regex in the beginning and code samples would have helped with your aversion to regex.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  5. #5
    SitePoint Zealot
    Join Date
    Jan 2006
    Posts
    169
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cool. The redirect condition is now being detected and is now calling the new URL. However, for the page url to be rewritten correctly I need to send the redirect to the old style url like in the example in my first post.

    From:
    mysite.com/-c-###_###_###

    to:
    mysite.com/index.php?cPath=###_###_###

    The ###_###_### is a series of category IDs that shows the full path to the current category (there can be from 1-n ### in a given string). How do I separate the ###_###_###_... from the -c- when appending the string to the new URL?

    Thanks again for all your help. I'm ordering a book on Regex so I am not so challenged next time I encounter this...

    Now the [QSA,L] directive is appending -c-###_###_### to the new url. What I need is to remove the "-c-" from the string that is appended.

  6. #6
    SitePoint Zealot
    Join Date
    Jan 2006
    Posts
    169
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    After re-reading your document and playing around with it, I am now using the following:

    RewriteRule ^/?-c-([0-9_]+).html$ index.php?cPath=$1 [QSA,L]

    It is displaying the correct page, but the URL is not being reformated like the

    RewriteRule ^(.*)-c-(.*).html$ index.php?cPath=$2&%{QUERY_STRING}

    rewrite string does. BTW, I will fix the formatting on that one, too.

  7. #7
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,607
    Mentioned
    19 Post(s)
    Tagged
    2 Thread(s)
    wm,

    Ah, the good news is that you're learning regex (as applied to URLs) but I can't determine if there was a follow-on question.

    For what it's worth (and this is repeated from an earlier thread), Master Regular Expressions by Friedl is the Bible of regex and The Definitive Guide to Apache mod_rewrite by Bowen is specific to mod_rewrite (although it applies mostly to httpd.conf).

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  8. #8
    SitePoint Zealot
    Join Date
    Jan 2006
    Posts
    169
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    DK,
    Thanks again. The unexpected behavior appears to be in my php code and not with the redirect. I think I have it all working properly now.

    Regards,
    Kenja

  9. #9
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,607
    Mentioned
    19 Post(s)
    Tagged
    2 Thread(s)
    wm,

    Yeah, that happens a lot, too!

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •