URL rewrite problem

Discussion in 'Apache' started by Matt18, Mar 15, 2012.

  1. #1
    Hi

    Can someone please help me out with the code below? Where is the problem?

    Thank you in advance!

    RewriteEngine On
    RewriteRule   ^(.*)$   index.php?product=$1   [NC,L]
    Code (markup):
     
    Matt18, Mar 15, 2012 IP
  2. ctrenks

    ctrenks Member

    Messages:
    17
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    38
    #2
    Is that the entire .htaccess file? looks like it should take the Entire url and make it a product.

    Can you reduce this to
    
    RewriteEngine On
    RewriteRule   ^/producs/(.*)$   index.php?product=$1   [NC,L]
    [code]
    
    and wahtever is after /producs/123-Allthistextwillbether
    
    will be index.php?product=123-Allthistextwillbether
    
    as now your getting a lot more, try to echo the $_GET[product] to see what your getting,.
    Code (markup):
     
    ctrenks, Mar 16, 2012 IP
  3. Matt18

    Matt18 Guest

    Messages:
    591
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Yeah, this works. But I don't want anything before the product's name. So I want ^(.*)$, not ^/producs/(.*)$. How can I make it happen? This is everything in my htaccess
     
    Matt18, Mar 16, 2012 IP
  4. ctrenks

    ctrenks Member

    Messages:
    17
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    38
    #4
    well if you have a fake directory or real for products what i put would only give you whats after products.. so unles every page other than the homepage is a product you would need to do somthing like that or parse the difference at pruduct.php

    post a few example urls here so we can see what you want to do exactly.
     
    ctrenks, Mar 17, 2012 IP
  5. Matt18

    Matt18 Guest

    Messages:
    591
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thank you for your time! I am trying to do something similar to wordpress url rewrite.

    So all url's such as domain.com/category/name-of-category/ would go to index.php?category=name-of-category . While everything else would go to index.php?product=$1 .

    I was looking for a site that would explain wordpress redirects in details but I couldn't find it anywhere. Could you maybe give me a resource and I'll try to figure it out on my own.

    Thank you once again for trying to help me :)
     
    Matt18, Mar 18, 2012 IP
  6. ctrenks

    ctrenks Member

    Messages:
    17
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    38
    #6
    there are a ton of turorials out there. You need to look at both sides, if you have

    RewriteRule ^(.*)$ index.php?product=$1 [NC,L]

    Everything after domain.com/ would go to products even /categoey/categoryname/widget will end up as

    index.php?product=categoey/categoryname/widget and your not going to want that I dont think unless products parses the parts or you use this at the end as a final catch all rule.

    RewriteRule ^/category/(.*)$ index.php?product=$1 [NC,L]

    would at least take everything after domain.com/category/ and use it or even better would be

    RewriteRule ^/category/(.*)$/(.*)$/ index.php?category=$1&product=$2 [NC,L]

    taking domain.coim/$1/$2
     
    ctrenks, Mar 18, 2012 IP
  7. Matt18

    Matt18 Guest

    Messages:
    591
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    How does wordpress solve that problem? is there a detailed tutorial on how to duplicate wordpress rewrite rules?
     
    Matt18, Mar 19, 2012 IP
  8. Matt18

    Matt18 Guest

    Messages:
    591
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Is it possible to do something like

    if
    ^/category/(.*)$ index.php?category=$1 [NC,L]

    else
    ^(.*)$ index.php?product=$1 [NC,L]

    ?
     
    Matt18, Mar 19, 2012 IP
  9. ctrenks

    ctrenks Member

    Messages:
    17
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    38
    #9
    well, you could do many things

    You can use the final swithc after category, ie if this matches then stop procesing rules.

    I dont use wordpress but I supose they take every request to the index and process the url string from there.
     
    ctrenks, Mar 19, 2012 IP
  10. Matt18

    Matt18 Guest

    Messages:
    591
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Cool, thanks. I'll do that. But back to the original problem.

    ^(.*)$ index.php?product=$1 [NC,L]

    doesn't work for me :(

    I do that and then echo product variable in index file but what it actually echo is "index.php" instead of what I type after domain.com. Any idea what could be wrong?

    Thanks!
     
    Matt18, Mar 20, 2012 IP
  11. ctrenks

    ctrenks Member

    Messages:
    17
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    38
    #11
    are you echoing $_GET['product']

    you can try the QSA for a test as well instead of [NC,L] try [QSA]
     
    ctrenks, Mar 21, 2012 IP