Question about dynamic url using hyphen?

Discussion in 'Programming' started by mjewel, Apr 14, 2005.

  1. #1
    I am just learning to use php and am trying to fix some coding errors on database someone else created. I think I have most of the errors fixed, but have run into something that may or may not have been done wrong. :)

    This is probably a stupid question, but here goes:

    The php template I'm using is set up to access a MySQL database.

    <a href="http://www.foo.com/<?=$category?>/<?=$color?”>foo results</a>

    Everything works perfectly and there is no problem creating a dynamic url. The problem I have is that certain entries in the MYSQL data fields must contain two words, and all these are presently connected with an underscore i.e. “steel_widgets”

    An underscore works, produces a dynamic page correctly, and results in a url that looks like:

    http://www.foo.com/steel_widgets/purple

    For SEO purposes, I want a hyphen to show in the url instead of an underscore “steel-widgets”

    Any attempt to use a hyphen instead of an underscore breaks the string.

    Without using Apache Mod ReWrite, is this even possible?
     
    mjewel, Apr 14, 2005 IP
  2. J.D.

    J.D. Peon

    Messages:
    1,198
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Both, '-' and '_' are valid URL characters and need not to be escaped, according to the HTTP spec. The dash is a special characters in regular expressions (e.g. [a-z0-9]) - may be it gets misinterpreted somewhere in your validation/redirection/etc logic.

    J.D.
     
    J.D., Apr 14, 2005 IP
  3. J.D.

    J.D. Peon

    Messages:
    1,198
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Answering your question in 13667, there are three kinds of characters in URLs:

    1. Reserved: ; / ? : @ & = + $ ,

    These characters delimit URL parts. If you want to use any of these characters, you must escape them

    2. Unreserved: alphanum - _ . ! ~ * ' ( )

    These characters have no special meaning and can be used as is. Browsers don't escape these characters

    3. Excluded: control characters, space, < > # % ", { } | \ ^ [ ] `

    These must be escaped as well.

    J.D.
     
    J.D., Apr 14, 2005 IP