.htaccess question - query sting param redirect

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • LaoTzu
    Confirmed User
    • Feb 2004
    • 350

    #1

    .htaccess question - query sting param redirect

    $10 epass and my eternal gratitude for a working solution:

    I need to 301 redirect urls with a specific query string parameter (any value) to my main url. In other words:

    Code:
    http://www.domain.com/index.php?[B]cat[/B]=x
    http://www.domain.com/index.php?[B]cat[/B]=whatever
    redirects to:
    Code:
    http://www.domain.com
    (no file name (index.php), just the main domain.)

    but
    Code:
    http://www.domain.com/index.php?[B]dir[/B]=x
    stays unchanged.
  • nation-x
    Confirmed User
    • Mar 2004
    • 5370

    #2
    why use htaccess to do that? why not just use php?

    Comment

    • nation-x
      Confirmed User
      • Mar 2004
      • 5370

      #3
      put this at the top of your index.php

      Certain values
      PHP Code:
      if (isset($_GET['cat'])) {
          switch ($_GET['cat']) {
              case 'x':
                  header("HTTP/1.1 301 Moved Permanently");
                  header("location: http://www.domain.com");
                  header("connection: close");
                  break;
             case 'whatever':
                  header("HTTP/1.1 301 Moved Permanently");
                  header("location: http://www.domain.com");
                  header("connection: close");
                  break;
          }
      } 
      

      Any Value
      PHP Code:
      if (isset($_GET['cat'])) {
          header("HTTP/1.1 301 Moved Permanently");
          header("location: http://www.domain.com");
          header("connection: close");
      } 
      
      Last edited by nation-x; 02-02-2009, 04:58 AM. Reason: added any value redirect

      Comment

      • LaoTzu
        Confirmed User
        • Feb 2004
        • 350

        #4
        Yo, you're awesome for posting that, but no dice. I tried the code in index.php but it had no affect. This script has so many .php functions and external files - totally beyond my grasp. That's why I was hoping for a neat and tidy .htaccess fix.
        Hit me up with your epass though! mcurtis313 at gmail dot com

        Comment

        • drocd
          Confirmed User
          • Aug 2007
          • 128

          #5
          Code:
          RewriteEngine on
          RewriteCond %{QUERY_STRING} ^cat=.*$
          RewriteRule .* http://www.domain.com [R=301,L]
          230-699

          Comment

          • LaoTzu
            Confirmed User
            • Feb 2004
            • 350

            #6
            Originally posted by drocd
            Code:
            RewriteEngine on
            RewriteCond %{QUERY_STRING} ^cat=.*$
            RewriteRule .* http://www.domain.com [R=301,L]
            So close!! The redirected URL is appended with the query string.
            http://www.domain.com/?cat=x

            I want to kill the trailing slash and query string for the canonical URL.

            Comment

            • drocd
              Confirmed User
              • Aug 2007
              • 128

              #7
              Okay, then change it to this:
              Code:
              RewriteEngine on
              RewriteCond %{QUERY_STRING} ^cat=.*$
              RewriteRule .* http://www.domain.com? [R=301,L]
              230-699

              Comment

              • LaoTzu
                Confirmed User
                • Feb 2004
                • 350

                #8
                WINNER!
                I've still got the trailing slash (www.domain.com/) but that's damn close.
                Hit me up with your epass mcurtic313 a t gmail d o t com

                Comment

                • drocd
                  Confirmed User
                  • Aug 2007
                  • 128

                  #9
                  no epass needed. just hit me up if you need a programmer in the future.

                  note: even if you put the url in your browser without the trailing slash, you'll see it refresh WITH the slash.
                  230-699

                  Comment

                  • LaoTzu
                    Confirmed User
                    • Feb 2004
                    • 350

                    #10
                    You, sir, are the man. I will.

                    Comment

                    Working...