Help:how To Do The Redirect

Discussion in 'PHP' started by PinoyIto, Feb 15, 2013.

  1. #1
    Hello guys,

    Will you please help me to solve this problem. How to create a mod rewrite for this problem.

    i have the following url
    
    locate.php?a=numbers; //combination of numbers 0-9
    
    Code (markup):
    Instead of that url, I made it search engine friendly,
    the new url now is
    
    location/numbers.html; //combination of numbers 0-9
    
    Code (markup):
    I then create a mod re-write
    
    RewriteEngine  on
    RewriteRule ^location/([0-9-]*).html$  locate.php?a=$1 [L,NC]
    
    Code (markup):
    The above mod rewrite work fine... but my problem is
    When someone access directly the file
    locate.php?a=numbers; //combination of numbers 0-9
    How can I redirect someone else who open the locate.php to the search engine friendly url?

    example locate.php?a=12 -> location/12.html

    Thanks in advance
     
    PinoyIto, Feb 15, 2013 IP
  2. webwindow

    webwindow Banned

    Messages:
    83
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    23
    #2
    You can use if(parse_url($url, PHP_URL_QUERY)) which can be used to check whether there is query string in the url or not. If it wont work for you let me know, as its working for me fine
     
    webwindow, Feb 15, 2013 IP
  3. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #3
    It will be something like this:
    
    RewriteEngine  on
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-l
    RewriteRule ^locate.php?a=(.*)$ location/$1.html [R=301]
    RewriteRule ^location/([0-9-]*).html$  locate.php?a=$1 [L,NC]
    
    Code (markup):
     
    Last edited: Feb 15, 2013
    MyVodaFone, Feb 15, 2013 IP
  4. PinoyIto

    PinoyIto Notable Member

    Messages:
    5,863
    Likes Received:
    170
    Best Answers:
    0
    Trophy Points:
    260
    #4
    Thanks for the reply, but how can I do it any example?
     
    PinoyIto, Feb 15, 2013 IP
  5. PinoyIto

    PinoyIto Notable Member

    Messages:
    5,863
    Likes Received:
    170
    Best Answers:
    0
    Trophy Points:
    260
    #5
    Thanks mate, but not working
     
    PinoyIto, Feb 15, 2013 IP
  6. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #6
    Slight change... the trick is [R=301] for redirect
    RewriteEngine  on
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-l
    RewriteRule ^locate.php?a=(.*)$ http://%{HTTP_HOST}location/$1.html [R=301]
    RewriteRule ^location/([0-9-]*).html$  locate.php?a=$1 [L,NC]
    Code (markup):
     
    MyVodaFone, Feb 15, 2013 IP
  7. webwindow

    webwindow Banned

    Messages:
    83
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    23
    #7
    Have you tried this keeping this code on the top line of your php page?

    if(isset($_GET['a']))
    {
    header("Location:location/".$_GET['a'].".html";
    }
    PHP:
     
    webwindow, Feb 15, 2013 IP
  8. PinoyIto

    PinoyIto Notable Member

    Messages:
    5,863
    Likes Received:
    170
    Best Answers:
    0
    Trophy Points:
    260
    #8
    still no success seems I miss something with this, I know this is almost near like wordpress mod
     
    PinoyIto, Feb 15, 2013 IP
  9. PinoyIto

    PinoyIto Notable Member

    Messages:
    5,863
    Likes Received:
    170
    Best Answers:
    0
    Trophy Points:
    260
    #9
    With this solution I keep getting the following browser error message seems infinite loop
    The page isn't redirecting properly
     
    PinoyIto, Feb 15, 2013 IP
  10. PinoyIto

    PinoyIto Notable Member

    Messages:
    5,863
    Likes Received:
    170
    Best Answers:
    0
    Trophy Points:
    260
    #10
    Thanks for the help guys... I figured it out...
     
    PinoyIto, Feb 15, 2013 IP
  11. King Kovifor

    King Kovifor Active Member

    Messages:
    19
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    75
    #11
    Reason you are getting an infinite loop here is that when you hit /locations/[0-9].html it actually renders it as locate.php?a=[0-9], so $_GET['a'] is always set, and ends up in an infinite loop to itself.
     
    King Kovifor, Feb 17, 2013 IP