SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Member
    Join Date
    Dec 2012
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    RewriteRule with Multiple Variables not parsing correctly

    Hello,

    I'm having trouble with a Apache Rewrite rule that needs to pass two variables. It should be simple enough, but the output is not parsing the variable properly.

    Here's the rule as I'm using it:
    RewriteRule ^/?(category)/([A-Za-z-]+)/([0-9-]+)/?$ /index.php?c=$2&start=$3 [L]

    essentially, the input would be: http://www.example.com/category/dogs/2

    which should internally be http://www.example.com/index.php?c=dogs&start=2

    instead, what i get is http://www.example.com/index.php?c=dogs/2&start=

    why are $2 and $3 not parsing?

    Thanks in advance...

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

    Welcome to SitePoint!

    While I object to your mod_rewrite code on a number of technicalities, there is nothing to make it perform as you've indicated so it's my guess that there is a typo in either what you've requested of your browser OR what you've presented above.

    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
    Dec 2012
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, it was a typo. Lost a few hairs, but i figured it out.

    This works:
    RewriteRule ^/?cateogry/([A-Za-z-]+)?/([0-9-]+)/?$ /index.php?c=$1&start=$3 [L]

  4. #4
    SitePoint Member
    Join Date
    Dec 2012
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    btw, why do you "object" to this code? security? speed?

  5. #5
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,097
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by stnman View Post
    btw, why do you "object" to this code? security? speed?
    First the ^/? at the start is used when you don't know whether you are using Apache 1.x or Apache 2.x (1.x requires a / at the start, 2.x does not). Once you know, adjust your code accordingly.

    Second, the ? at the end of ([A-Za-z-]+)? makes no sense at all, unless you really do have a category without a name. Drop that at the end there.

    Third, the /? at the end is a bit weird. It's up to you to determine what your URLs are, but now they work both with and without a trailing slash, which may lead to a duplicate content penalty from search engine spiders. I you must allow both with and without it's better to 301 redirect one to the other.

    Forth, the / in front of /index.php makes Apache look in the root of the filesystem first to see if the requested file is there. Only when that fails will it look at the root of your web directory. This slows things down unnecessarily, so remove the leading / there.

    Lastly, $3 should be $2.

    All in all, this would be the end result:

    Code:
    RewriteRule ^cateogry/([A-Za-z-]+)/([0-9-]+)$ index.php?c=$1&start=$2 [L]
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  6. #6
    SitePoint Member
    Join Date
    Dec 2012
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the suggestion - I'm using Apache2
    I tried the rule with your changes, and yes it parses correctly when the category name is one word. However, if there's a category with two or more names, with spaces, the browser forces a search.
    e.g. category = Blue Room http://www.example.com/category/blue room

    if I use the orgiinal rule with the ^/?category it works. With ^category it doesn't.

    I'm still a novice at apache rules and i see you can parse spaces with %20 but it doesn't seem to work for me. any suggestions?
    thanks again.

  7. #7
    SitePoint Member
    Join Date
    Dec 2012
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quick update - i cleared my browser cache in Firefox, and then it worked. In Chrome it worked first time. Thanks again for the suggestions.

  8. #8
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,097
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    You can't use spaces in URLs as you've found out, only %20, but that looks a bit stupid IMHO.
    What most people do is replace spaces with dashes, so the URL would become /category/blue-room
    The "blue-room" part of that URL is called a "slug", which you normally store in the database (i.e. rewrite the category name to a slug when a category is created/updated) along with the record for Blue Room for easy lookup.
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  9. #9
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,679
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    Rémon's answer in post #5 was spot on! It only missed the confusion you'd be causing if you allow the trailing / because visitors COULD be at two levels in your file structure (resulting in relative links being unable to handle both cases). IMHO, trailing /'s should be reserved to the intended purpose (identifying a directory).

    Rémon's response in post #8 omitted my signature's tutorial about replacing spaces with _'s when creating the links and changing back before querying the database. Yes, many people love -'s but that would eliminate the possibility of hyphenated words, wouldn't it?

    The bottom line is that my signature's tutorial has been online for years, it's been turned into a SitePoint article and it's the basis for several sticky threads in this board. Take advantage of the information available (and ask the author directly if you have questions).

    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

  10. #10
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,314
    Mentioned
    19 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by dklynn View Post
    many people love -'s but that would eliminate the possibility of hyphenated words, wouldn't it?
    Yes, it would. But on the other hand, hyphens are better for search indexing, and I think most people consider that a more important factor.
    "First make it work. Then make it better."


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
  •