SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    May 2005
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question URL Rewriting Help

    Hi All,

    My .htaccess have these entries before some other entries

    RewriteRule ^([a-z0-9]+)$ /myfiles/listpro.php?search=$1 [NC]
    RewriteRule ^([a-z0-9]+)/(.*)$ /myfiles/listpro.php?search=$1&page=$2 [NC] [L]

    When someone searches by a keyword eg:Macintosh, the url must be http://www.mysite.com/Macintosh
    It is working fine.

    But i have pagination implemented on these pages and when it is clicked the server falls in an infinite loop and breaking it

    http://www.mysite.com/Macintosh/recent/11 as i my second rewrite rule
    Does my rule right and also is listing in the right order?

    The other entries preceding this are

    RewriteRule web/(.*)$ /myfiles/listpro.php?cat=web&page=$1
    RewriteRule web /myfiles/listpro.php?cat=web [L]

    like web there are other entries like intranet, pc, etc ( and these are fixed categories unlike search keywords entered by the user)
    These all are above the Macintosh entry and works fine even with pagination

    Pls help me to sort this out

    Thanks

    sgcet

    Tuesday, May 24 2005 06:20 AM

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

    Loop: Do you think that ([a-z0-9]+) can match myfiles and (.*) can match listpro.php? THAT IS WHY I HATE THE EVERYTHING ATOM!

    If you can only accept digits in the second atom, why did you allow EVERYTHING? Change that to ([0-9]+) and that will resolve the looping problem.

    Ditto the EVERYTHING atom in the listpro.php?cat=web&page=$1 redirect.

    Please have a read of the article at datakoncepts.com/seo so you can understand how to assemble proper regex and come back with questions that were left unanswered.

    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 Member
    Join Date
    May 2005
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi DK,

    the 2 params do not need to match myfiles and listpro.php but rather the other way around.The regex had to match /Macintosh/recent/11 in the http://www.mysite.com/Macintosh/recent/11

    Regards
    sgcet

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

    Okay, I'll type slowly:

    Your regex,

    Code:
    ^([a-z0-9]+)/(.*)$
    matches

    Code:
    /myfiles/listpro.php?search=$1&page=$2 [NC] [L]
    BTW, [NC] [L] ***MUST*** be written [NC,L]
    To get your rewrite to land on myfiles/listpro.php without looping, you'll have to use RewriteCond like this:
    Code:
    RewriteCond %{REQUEST_URI} !^myfiles/listpro.php$
    RewriteRule ^([a-z0-9]+)/(.*)$ /myfiles/listpro.php?search=$1&page=$2 [NC,L]
    Do you see the loop? If not, remember that myfiles IS MATCHED by ([a-z]+) and (.*) will match EVERYTHING (listpro.php). THAT is where your loop is. By using the RewriteCond(ition), you prevent the loop with the condition that $1 cannot be myfiles AND $2 cannot be listpro.php.

    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 Member
    Join Date
    May 2005
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi DK,

    thanks for your reply.That was fast.May God bless you.
    I tried it another way before u replied and it is working perfectly. Don't know it will break in the future since there are a lot more rules coming up on the site.
    I will definetly hav to consider your RewriteCond some time in the future

    Right now my .htacess goes like this and in that order

    RewriteRule web/([a-zA-Z]+)/([0-9]+)$ /myfiles/listpro.php?cat=web&type=$1&page=$2 [L]
    RewriteRule web /myfiles/listpro.php?cat=web [L]

    and then in place of web others like intranet, pc ( 10 of them ) follows
    as i said this cat are fixed

    and then there will search criteria entered by the user and it can be anything

    For that i put this

    RewriteRule ^([a-z0-9]+)$ /myfiles/listpro.php?search_str=$1 [NC] [L]
    RewriteRule ^([a-z0-9]+)/([a-z]+)/([0-9]+)$ /myfiles/listpro.php?search_str=$1&type=$2&page=$3 [NC] [L]

    oops, sorry to put as [NC,L] but it works!!

    If u have any valuable sugestions to optimiise my htaccess pls help me

    Thanks Again'
    sgcet

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

    You're welcome - that's what they pay me the big bucks for around here! I wish!

    Hey, "if it ain't broke, don't fix it!"

    When you get a chance to play around with your .htaccess, give this a try:
    Code:
    RewriteEngine Off
    # All your existing code
    RewriteEngine On
    RewriteRule ^([a-z0-9]+)$ /myfiles/listpro.php?search=$1 [NC]
    RewriteCond %{REQUEST_URI} !^myfiles/listpro.php$
    RewriteRule ^([a-z0-9]+)/(.*)$ /myfiles/listpro.php?search=$1&page=$2 [NC,L]
    # add your latest, too, if that serves a useful purpose
    RewriteRule ^([a-z0-9]+)/([a-z]+)/([0-9]+)$ /myfiles/listpro.php?search_str=$1&type=$2&page=$3 [NC,L]
    The reason your [NC] [L] is working must be that mod_rewrite discards everything after the fourth arguement (RewriteRule - space - regex - space - redirect - space - optional_flags) so that the [L] flag is being lost (to no great effect, apparently).

    You have a good handle on regex ranges but must learn when to use the dreaded EVERYTHING atom (yes, there ARE some times to use it but, as you've discovered, it can very easily cause a loop). May I suggest a review of the article at datakoncepts.com/seo which will give you setup, test, regex and a warning about relative links. It may fill in a gap here or there for you.

    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
  •