Fork me on GitHub

pnAmazon and Sinple URLs - Hyphen in Module / URL name issue  Bottom

Go to page 1 - 2 [+1]:

  • Here is an interesting problem to solve.
    If you have simple URLs working and an .htaccess file.
    With the pnamazon module as an example, if you name your module Amazon, have your rewrite rules as below, you would think it should bring the URL for the BOOKS ASIN regardless of what name you name it, if you give it a name with a hyphen, it will only when there is at least 1 letter in the asin, if all the asin is 100% numbers, you'll have the URL rewritten, but nothing will come up. I am using optimised postnuke .73.
    The simple URL and rewrite code, everything works OK, except when it comes to books, only books with a mixture of letters and numbers or at least one letter and the rest numbers will work IF I HAVE A HYPHEN IN THE NAME, see below!

    Here is an example:

    Code

    # asin url
    RewriteRule ^blabla-([a-zA-Z0-9_]+).html$ modules.php?op=modload&name=Amazon&file=index2&asin=$1 [L]


    Then have this line in the $INURL. in the replace_for_mod_rewrite:

    Code

    $prefix . 'modules.php\?op=modload&(amp;)?name=Amazon&(amp;)?file=index2&(amp;)?asin=([a-zA-Z0-9_]+)"|',


    I tried ([^&]+), ([a-zA-Z0-9_]+), ([a-zA-Z0-9_+]+), ([a-zA-Z0-9-]) etc..

    For your $OUTURL you have :

    Code

    '"blabla-.html"',


    Basically, it'll work if YOU take the hyphen away, like this

    htaccess: ^blabla([a-zA-Z0-9_]+).html$ modules.php?op=m....

    $OUT: '"blabla$5.html"',

    I need the hyphen in!

    Interesting also, when using MDPro with short urls on, there is no problem. I'll keep hammering at it until I find the problem, I have the feeling the problem may not be a postnuke problem but the reference to alphanumeric characters or the order of the places in he URL....
  • Try using the following for your out?

    Code

    '"blabla-.html"',


    Slugger
  • That's what I used, it'll work but not for books with 100% ASIN numbers. When you take the hyphen away, it'll work!
  • '"blabla-$1.html"', instead of '"blabla-$5.html"', does not make any difference and will only bring a dash with no value after it, 5 in my case is the right one!
  • Just for grins try the following for $in

    Code

    $prefix . 'modules.php\?op=modload&(?:amp;)?name=Amazon&(?:amp;)?file=index2&(?:amp;)?asin=([\w\d]*)"|',


    and the following for $out

    Code

    '"blabla-.html"',


    and the following in your .htaccess file

    Code

    RewriteRule ^blabla-([a-zA-Z0-9]*)\.html$
    modules.php?op=modload&name=Amazon&file=index2&asin=$1 [L,NS,NC]


    Slugger
  • dusky

    Here is an example:

    Code

    # asin url
    RewriteRule ^blabla-([a-zA-Z0-9_]+).html$ modules.php?op=modload&name=Amazon&file=index2&asin=$1 [L]


    Then have this line in the $INURL. in the replace_for_mod_rewrite:

    Code

    $prefix . 'modules.php\?op=modload&(amp;)?name=Amazon&(amp;)?file=index2&(amp;)?asin=([a-zA-Z0-9_]+)"|',

    The problem is that the set of valid characters for the asin is defined by the character class [a-zA-Z0-9_]. That means that you can have lower case letters, upper case letters, numbers and underlines. Anything else and it won't match. If you want to allow hyphens, then you need to change the character class to [-a-zA-Z0-9_]. That's because hyphens inside the character class definition are meta characters and so you need to be careful how to include a hyphen. (Putting it as the first character of a character class works because it couldn't possibly specify a range of characters if it's the first character. :D )

    Now, if the asin has a more specific format than any combination of letters, numbers, hyphens and underlines, we could write a more specific regex for you...

    Otherwise, just change the character class wherever it's needed in the functions.php and .htaccess.

    Important: You need to be careful because hyphen is also used to separate parameters in the Simple URL. That might cause problems recognizing which parameter is which. So test your changes very thoroughly.
  • Quote


    Important: You need to be careful because hyphen is also used to separate parameters in the Simple URL. That might cause problems recognizing which parameter is which. So test your changes very thoroughly.


    The ASIN does not have a hyphen, it's only my choice to include it in the URL. I have tried specifying it in [-a-zA-Z0-9_] etc, but no success. As I said, if I don't include it. it'll work, If Ido, it'll work only if the ASIN has one or more letters with numbers
  • I'm sorry, I've mis-understood your problem. Could you restate it?
  • I want the URL modules.php?op=modload&name=Amazon&file=index2&asin=ASINHERE to be rewritten as: something-ASINHERE.html, notice the hyphen!

    The result I get is it gets rewritten, when the book ASIN is numbers and letters (example: something-147132282VL.html), it works (it brings the page for the book when you click the URL). When the book ASIN is all numbers (example: something-14713228298.html) it does not bring the book page but the "Your search term returned no results or we...."

    The only way to understand the problem, may be is to set up a demo module and try to duplicate the problem.
    I tried it on other sites of mine with postnuke .76+, the same thing hapens.
    As I see it, either apache / PHP through the htaccess file understanding the hyphen as a MINUS sign when it is preceding all numbers, but treated as a character when preceding letters and numbers (a combo of numbers and letters can't be mathematically summed, hence treat as a hyphen and no error)

    I tried escaping the hyphen in different places, no success. I am sure I am looking at the answer but can't put my finger on it!

    PS: ASIN numbers above are just examples.

    This problem I think may be of relevance to other modules and even outside the postnuke scope. When faced with the same scenario, finding a solution probably means a lot than we think!
  • dusky

    I want the URL modules.php?op=modload&name=Amazon&file=index2&asin=ASINHERE to be rewritten as: something-ASINHERE.html, notice the hyphen!

    The result I get is it gets rewritten, when the book ASIN is numbers and letters (example: something-147132282VL.html), it works (it brings the page for the book when you click the URL). When the book ASIN is all numbers (example: something-14713228298.html) it does not bring the book page but the "Your search term returned no results or we...."

    Let's stop with the examples and start using actuals. What is the actual simplified URL that fails? What is the actual rewrite rule in the .htaccess file?

    Quote


    The only way to understand the problem, may be is to set up a demo module and try to duplicate the problem.

    No, we just need to debug your implementation. I'm sure that we can figure this out.

    Quote


    As I see it, either apache / PHP through the htaccess file understanding the hyphen as a MINUS sign when it is preceding all numbers, but treated as a character when preceding letters and numbers (a combo of numbers and letters can't be mathematically summed, hence treat as a hyphen and no error)

    Good try but that's not how the regex engine (that rewrite uses to interpret the .htaccess rewrite rules) works. It just looks at the URL as a string of characters.

    Quote


    This problem I think may be of relevance to other modules and even outside the postnuke scope. When faced with the same scenario, finding a solution probably means a lot than we think!

    Actually, it's probably just some unexpected combination of rules that is causing the problem. With a little debugging, I'm sure we'll figure it out.
  • CRS is probably right. Most of the issues that I've had with the ShortURLs rewriting has come from the order that the rules are listed in.

    To wit: if you have a more generalized before the specific one(s) you are writing, the first applicable rule found is applied, and the rest no longer recognize the entry. If it is not already, try putting your rule at the top of the stack.
  • I'd be happy to help... Just send the URL and rewrite rule... Or send it to me in email to crs AT mtrad DOT com.
  • Hi CSR!

    'scuse me and I don't want to hijack this tête à tête but is there something wrong with the rules I wrote? They seem to work just fine but I know there's always that one overlooked exception. Is that the case?

    Slugger

    PS Thanks for your stats module. You're a stud!
  • Hi CRS!

    I don't want to hijack this tête à tête but is there something wrong with the rules I wrote? They seem to work just fine but I know there's always that one overlooked exception. Is that the case?

    Slugger

    PS Thanks for your stats module. You're a stud!
  • Slugger

    I don't want to hijack this tête à tête but is there something wrong with the rules I wrote? They seem to work just fine but I know there's always that one overlooked exception. Is that the case?

    No problem. I didn't mean to ignore your answer. I was just focused on what dusky was trying to say or resolve.

    In looking at your rule, it looks okay to me. There are a couple of things to note:

    * You don't need the \d in the character class, becaused numeric digits are already included inthe \w.

    * I am used to using a-zA-Z0-9_ instead of \w because some versions of regex don't include the underscore. I can't keep track of them, so I just don't use \w.

    * The use of * instead of + means that the asin can be empty.

    But the most important evaluation is whether it works.... and you say that it has been. So that's what counts. wink

Go to page 1 - 2 [+1]:

This list is based on users active over the last 60 minutes.