Does PHP or MySQL a problem with -in-

UKSBD

Moderator
  • Dec 30, 2005
    13,043
    1
    2,840
    My directory has sections for Business Categories in Postal Town.

    I've just noticed /Category-in-Henley-in-Arden/ doesn't work.

    On looking in to this I've discovered other towns that have -in- in them don't work either
    Moreton-in-Marsh, Barrow-in-Furness, Kirkby-in-Furness (fortunately, not many)

    Towns with -on-, -upon-, -under- all work fine.

    Is there something in PHP or MySQL that could be effecting -in- ?
     
    Any sort of category checking should work one of 2 ways, by a unique ID is the best approach, or if you have a less professional programmer you would be dealing with exact string searches.

    However neither would cause any trouble with -in-, so the only thing that can be wrong is that either an ID is not referenced correctly, or if your script is using exact string then that string is not correct.
     
    Upvote 0

    UKSBD

    Moderator
  • Dec 30, 2005
    13,043
    1
    2,840
    nothing wrong with - in urls in php, WordPress and many other scripts used them fine (even this websites forum software)

    Your issue is likely with the htaccess redirect somewhere

    Yes, it looks like a rewrite problem, it works without the redirect.

    This is what I have as the Rewrite
    RewriteRule ^intown/(.*)-in-(.*)\/$ category-town.php?strBusinessCatergory=$1&strTown=$2

    This works for any town except the ones with -in- in their name

    https://www.uksmallbusinessdirectory.co.uk/intown/Copywriters-in-Bristol/ works
    https://www.uksmallbusinessdirectory.co.uk/intown/Copywriters-in-Henley-in-Arden/ doesn't work

    Any idea why the -in- in the town is causing this?
     
    Upvote 0
    F

    Faevilangel

    Edited:

    I reread your comment and the issue is far harder, I am guessing the regex is breaking because of the -in- in the first part and the last part of the url, my regex sucks so you are better off asking on stackoverflow.com as you will likely need to build it into 2 parts and then join them.
     
    Last edited by a moderator:
    Upvote 0

    pjperez

    Free Member
    Mar 31, 2014
    106
    17
    Reading
    Yes, it looks like a rewrite problem, it works without the redirect.

    This is what I have as the Rewrite
    RewriteRule ^intown/(.*)-in-(.*)\/$ category-town.php?strBusinessCatergory=$1&strTown=$2

    This works for any town except the ones with -in- in their name

    https://www.uksmallbusinessdirectory.co.uk/intown/Copywriters-in-Bristol/ works
    https://www.uksmallbusinessdirectory.co.uk/intown/Copywriters-in-Henley-in-Arden/ doesn't work

    Any idea why the -in- in the town is causing this?

    You have two variables: $1 which is the business category and $2 which is the town. My understanding is that your application gets a request like /Copywriters-in-Bristol/ and then you capture whatever is before -in- as the category ($1) and whatever is after -in- as the town. However, if you have the request /Copywriters-in-Henley-in-Arden/ your application might be looking for Copywriters ($1) in Arden ($2) or "Copywriters-in-Henley" as category and again Arden as the city.

    As there's a redirect and I'm guessing some sort of DB query for the request, you should be able to review your Apache's error logs and/or logs on your DB to see what's the application actually doing.

    Without that and/or a code review it's difficult to tell.

    The regex looks good... I'm not 100% sure about the not escaped / after intown, but if it works for you it must be ok.
     
    Upvote 0

    UKSBD

    Moderator
  • Dec 30, 2005
    13,043
    1
    2,840
    It doesn't appear to be the -in- between the 2 variables that is causing the problem.
    I created a version just using - between them and it's still the same.

    It seems to be because the 2nd variable has -in- in it.
    It may be confused and think there are 3 variables and not 2
    Copywriters in Henley in Arden
    instead of Copywriters in Henley-in-Arden

    It only does it with towns that have -in- in them, -on-, -upon-, -under- all work fine.
     
    Upvote 0

    UKSBD

    Moderator
  • Dec 30, 2005
    13,043
    1
    2,840
    A version of the regex? or a request like Copywriters-Henley-in-Arden?

    By the way, I'm assuming the regex shown above is for Apache and not in your application, right? Do you parse this anywhere else in your code?

    A request.
    https://www.uksmallbusinessdirectory.co.uk/catintown/Copywriters-Bristol/ works
    with Henley-in-Arden it doesn't

    This is the file without the rewrite
    https://www.uksmallbusinessdirector...Catergory=Copywriters&strTown=Henley-in-Arden

    As you can see the -in- doesn't cause a problem there, only with the rewrite.
     
    Upvote 0

    pjperez

    Free Member
    Mar 31, 2014
    106
    17
    Reading
    So here's how that regex matches for your general case:

    XeUAOC0.png


    Here's how it matches for any other -xn- e.g. -on-:

    aF1PtLZ.png


    Finally, here comes your edge case, where the regex takes Copywriters-in-Henley for $1 and Arden for $2:

    rZr48tX.png


    This is because (.*) is considered greedy, so it matches as many characters as possible. As we have two occurrences of -in-, the regex will push the match to the second -in- so the initial (.*) gets as many characters as possible.

    I haven't found a solution, but what we want here is to always match on the first -in- and not on the second.
     
    Upvote 0

    Latest Articles