PHP & HTaccess $_GET Issue

Discussion in 'PHP' started by LazyD, May 2, 2007.

  1. #1
    So this is really stumping me....Ive used this same method many times before without error, but its not working now....

    Im simply using htaccess to rewrite URLs and using PHP to $_GET the variables... Problem is, when I echo out the $_GET variable its echoing the word "index" instead of the word in the URL

    My htaccess:
    RewriteEngine on
    RewriteRule ^(.*)/(.*)\.php$ index.php?Category=$1&SubCat=$2 [L]
    RewriteRule ^(.*)\.php$ index.php?Category=$1 [L]

    My PHP file:
    $CategoryGet = $_GET['Category'];
    $SubCatGet = $_GET['SubCat'];

    echo $CategoryGet;

    Now, if I go to http://mydomain.com/Jerseys.php

    the echo outputs "index"

    Now, if i go http://mydomain.com/Jerseys/Youth.php

    the echo again, outputs "index"

    This also effects it even when I use index.php?Categoy=Jerseys

    When I remove the lines from the htaccess, it magically starts working again.... What the hell is going on..

    INTERESTING UPDATE ------

    When I change the following in my htaccess it works...
    RewriteRule ^(.*)\.html$ index.php?Category=$1 [L]

    Changing the destination URL from PHP to HTML fixes it.... However, I want them to .php...

    Another update.... I tried changing it to the following:
    RewriteRule ^(.*)\.php4$ index.php?Category=$1 [L]

    That works as well, for whatever reason, it seems that this thing is retarded and cant handle a plain .php
     
    LazyD, May 2, 2007 IP
  2. nabil_kadimi

    nabil_kadimi Well-Known Member

    Messages:
    1,065
    Likes Received:
    69
    Best Answers:
    0
    Trophy Points:
    195
    #2
    Then use ".html"
     
    nabil_kadimi, May 3, 2007 IP
  3. rodney88

    rodney88 Guest

    Messages:
    480
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #3
    It's not retarded in the slightest. You're telling it to rewrite all requests for .php files to another .php file (the index.php file).

    The request for index.php?Category=originalcat will in turn be rewritten again to index.php?Category=index

    You can either put the rewrite code into your httpd.conf and the [L] flag will stop processing after the first rewrite (as .htaccess are only interpreted perdir, Apache has to make another request for the new location in case any more rewrites apply).

    Or add in a RewriteCond to check your filename against already being the index script. I.e.
    RewriteCond %{SCRIPT_FILENAME} !index\.php$
    RewriteRule ^(.*)/(.*)\.php$ index.php?Category=$1&SubCat=$2 [L]
    RewriteCond %{SCRIPT_FILENAME} !index\.php$
    RewriteRule ^(.*)\.php$ index.php?Category=$1 [L]
     
    rodney88, May 3, 2007 IP
    LazyD likes this.
  4. LazyD

    LazyD Peon

    Messages:
    425
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    rodney88, what your saying makes sense, however I have multiple other sites using the code posted above in some variation that works flawlessly...

    Also, from looking at your rewritecode its telling the code if the filename is not index.php then run this code. Now what happens when the file IS index.php


    Edit****

    It works, thank you!
     
    LazyD, May 3, 2007 IP
  5. rodney88

    rodney88 Guest

    Messages:
    480
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #5
    As you said, a RewriteCond tells the rewrite engine to only apply the following RewriteRule if the specified condition is true. In this case, if the filename is not already index.php.

    When the RewriteCond is not met (i.e. it is index.php), the RewriteRule does not apply so in this case nothing happens.

    I'm not sure how you can have the same code working on other sites without encountering this problem - unless the script name which processes the requests does not also match the regex pattern, (i.e. by using different file extensions).

    Glad to hear its working now.
     
    rodney88, May 3, 2007 IP
  6. LazyD

    LazyD Peon

    Messages:
    425
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Well, come to think of it, the one of the others uses .html extension and another puts some other junk in front of it...
     
    LazyD, May 3, 2007 IP
  7. LazyD

    LazyD Peon

    Messages:
    425
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Now im having a new problem....

    I have links for each product on the page, that go to a cart.php file, everytime I click one, it keeps redirecting back to the index.php, I know the htaccess is the issue because I removed the lines and it works...
     
    LazyD, May 3, 2007 IP
  8. LazyD

    LazyD Peon

    Messages:
    425
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Screw it, ill just rewrite them as html...
     
    LazyD, May 3, 2007 IP
  9. VimF

    VimF Well-Known Member

    Messages:
    307
    Likes Received:
    27
    Best Answers:
    0
    Trophy Points:
    118
    #9
    ".php" doesn't work because you used the rule "(.*)" which includes "index" --> It'll cause an endless loop.
     
    VimF, May 3, 2007 IP
  10. LazyD

    LazyD Peon

    Messages:
    425
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Is there anyway I can get around that?

    I want to keep all of my pages extensions as php, I really dont want to have some pages as html, or php4, or whatever, and then some have normal .php

    Edit - Fine fine fine, ive decided to use the following as my permanent solution...

    RewriteEngine on
    RewriteRule ^Gear/(.*)/(.*)\.php$ index.php?Category=$1&SubCat=$2 [L]
    RewriteRule ^Gear/(.*)\.php$ index.php?Category=$1 [L]

    Prefix the URL with Gear to alleviate the issue..

    Thank you to all that helped
     
    LazyD, May 3, 2007 IP