Mod Rewrite ENDLESS LOOP headache

Discussion in 'Apache' started by zak, Oct 19, 2006.

  1. #1
    Hi

    Just a beginner with all this mod rewrite stuff!

    I've been looking for a solution for days without any luck!

    I want to redirect the url

    example.com/catalogue.php?ctn=whatever (the page cached in search engine)

    TO

    example.com/catalogue/whatever.html

    Which is quite easy like so:

    RewriteRule ^catalogue/([^/]*)\.html$ /catalogue.php?ctn=$1 [L]


    The problem is
    you can still go to catalogue.php?ctn=whatever which is also cached in search engine indexes.
    So I need to redirect - issue a 301 from
    catalogue.php?ctn=whatever
    TO
    catalogue/whatever.html
    WITHOUT ENDING UP IN A NEVER ENDING LOOP BECAUSE OF THE 1ST RULE

    HELP
     
    zak, Oct 19, 2006 IP
    GTech likes this.
  2. GTech

    GTech Rob Jones for President!

    Messages:
    15,836
    Likes Received:
    571
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Put something like this at the top of your catalogue.php:

    
    $ctn = $_GET["ctn"];
    if ($ctn > '') {
          header("HTTP/1.1 301 Moved Permanently");
          header("Location: http://www.yoursite.com/catalogue/" . $ctn . ".html");
          exit;
    }
    
    Code (markup):
     
    GTech, Oct 19, 2006 IP
  3. zak

    zak Peon

    Messages:
    175
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for the reply.

    If I put the above code in my php file it will redirect to the html page right?

    Will the first rewite rule kick in again and redirect back to the php page and so on....
     
    zak, Oct 19, 2006 IP
  4. GTech

    GTech Rob Jones for President!

    Messages:
    15,836
    Likes Received:
    571
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Should work fine. I basically copied this from an osCommerce site I maintain where I had to do this very same thing.

    It shouldn't give you any problems, but if it does, there is another way to accomplish it and I can help you code that as well.
     
    GTech, Oct 19, 2006 IP
  5. zak

    zak Peon

    Messages:
    175
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Will try and post back results!
    Thanks
     
    zak, Oct 19, 2006 IP
  6. zak

    zak Peon

    Messages:
    175
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #6
    No, that didnt work!

    It just doesnt do anything!
     
    zak, Oct 19, 2006 IP
  7. zak

    zak Peon

    Messages:
    175
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #7
    OK

    Error I get is 'server will never finish redirecting', so its ending up in a loop again!
     
    zak, Oct 19, 2006 IP
  8. Smaaz

    Smaaz Notable Member

    Messages:
    2,425
    Likes Received:
    160
    Best Answers:
    0
    Trophy Points:
    250
    #8
    Why don't you rename the catalog.php to cataglognew.php and then do something like...

    RewriteRule ^catalogue.php?ctn=([^/]*)$ /catalogue/$1.html [R=301,L]
    RewriteRule ^catalogue/([^/]*)\.html$ /cataloguenew.php?ctn=$1 [L]
     
    Smaaz, Oct 19, 2006 IP
  9. zak

    zak Peon

    Messages:
    175
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #9
    I have to say...

    Y didnt i think of that!!!!!

    Will try and follow up!

    Thanks
     
    zak, Oct 19, 2006 IP
  10. GTech

    GTech Rob Jones for President!

    Messages:
    15,836
    Likes Received:
    571
    Best Answers:
    0
    Trophy Points:
    0
    #10
    zak, another way to accomplish this:

    
      $url = explode('=',$_SERVER['REQUEST_URI']);
      $ctn = $url[1];
      if ($ctn > "") {
        header("HTTP/1.1 301 Moved Permanently");
        header("Location: http://www.yoursite.com/catalogue/" . $ctn . ".html");
        exit;
      }
    
    Code (markup):
     
    GTech, Oct 19, 2006 IP
    Mia likes this.
  11. vishwaa

    vishwaa Well-Known Member

    Messages:
    271
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    138
    #11
    Here is one kind of solution to issue 301 redirect for direct file requests.

     
    vishwaa, Oct 19, 2006 IP
    PinotNoir likes this.
  12. zak

    zak Peon

    Messages:
    175
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #12
    OK, got it working and its quite a simple way to do it

    copy catalogue.php to catalog.php or any other name you like.

    change your existing rewrite rule to
    RewriteRule ^catalogue/([^/]*)\.html$ /catalog.php?ctn=$1 [L]
    Code (markup):
    then add

    RewriteCond %{SCRIPT_FILENAME} catalogue\.php
    RewriteCond %{QUERY_STRING} ctn=([^/]*)
    RewriteRule ^.*$ http://www.example.co.uk/catalogue/%1.html? [R=301]
    Code (markup):
    :cool: self explanatory
     
    zak, Oct 19, 2006 IP