Hi, I am quite new in mod rewrite. I have two pages first page name Pet.php and the second page name EditPet.php

In Pet.php there are link for example
Dog when user click, it wil go to
http://localhost/Pro/EditPet.php?ID=1

Cat when user click, it wil go to
http://localhost/Pro/EditPet.php?ID=2

Rat when user click, it wil go to
http://localhost/Pro/EditPet.php?ID=3

But when the user click I want to use mod rewrite ..... and i want the result to be like
http://localhost/Pro/EditPet.php/1
http://localhost/Pro/EditPet.php/2
http://localhost/Pro/EditPet.php/3

My problem is I don't know what should I write in .htaccess file. I tried this but

RewriteEngine on
RewriteRule ^EditPet.php?ID=$1 /EditPet/([0-9]+)$

But I still got the result like http://localhost/Pro/EditPet.php?ID=1
http://localhost/Pro/EditPet.php?ID=2
http://localhost/Pro/EditPet.php?ID=3 🙁

Anyone have any advice .....

    I'll be the first to admit I don't know much about .htaccess files. However, a couple days ago, I needed to use one just like the one you're talking about. So I researched it, and got it working.

    From the looks of yours, it's just backwards. I think it should be like this:

    Options +FollowSymLinks
    RewriteEngine on
    RewriteRule EditPet.php?ID=([0-9]+)$ /EditPet/$1
    

    If you're wondering what i did, first I took out the ^ from EditPet.php (not sure if that's correct, but there may be a / before the E so it's probably better to just leave it out). Then I swapped the ([0-9]+)$ and $1, which makes sense if you think about it. You want to match when there's a digit in ID=whatever, not match when there's a digit in /EditPet/whatever.

    Let me know how that works out.

      I already tried your code. I also tried something like this

      RewriteEngine On 
      RewriteRule ^Pro/EditPet\.php/([0-9]+)/?$ /Pro/EditPet.php?ID=$1 [L] 

      But there still have other problems. Normally in Pet.php I have code like this

      <A HREF="EditPet.php?ID=<?=$row["ID"]?>"><?=$row["PetName"]?></A>

      and in EditPet.php I have one sql that need to use ID ...like this

      $sql="select * from PetShop where ID=$ID";

      I got advice from other people to change the link in Pet.php to be like this

      <A HREF="EditPet.php/<?=$row["ID"]?>/"><?=$row["PetName"]?></A>

      But this will generate error because in EditPet.php I have sql that need to use ID..... so what should I do. Any advice ? 🙁

        I think maybe I'll a little confused about what you're trying to do.

        Do you want the browser to display that they're at Pro/EditPet/# but really the server requests Pro/EditPet.php?ID=#?

        Do you want the browser to display that they're at Pro/EditPet.php?ID=#?

        Or is it something else?

        Maybe you can clear that up and I'll be able to help further.

          Hi,
          I want to broswer to display like

          http://localhost/Pro/EditPet.php/1
          http://localhost/Pro/EditPet.php/2

          But now it displayed like this

          http://localhost/Pro/EditPet.php?ID=1
          http://localhost/Pro/EditPet.php?ID=2

            In that case, the .htaccess that you posted at 4:18 should be fine. And yes, you should link like this:

            <A HREF="EditPet.php/<?=$row["ID"]?>/"><?=$row["PetName"]?></A> 
            

            You say it generates an error, but it shouldn't. What error does it generate?

              Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource

              Because in EditPet.php
              I have one line like

              $sql="select * from PetShop where ID=$ID"; 

              Because at first, I have ID that is sent with the link

              <A HREF="EditPet.php?ID=<?=$row["ID"]?>"><?=$row["PetName"]?></A>

              But once I change the code to
              <A HREF="EditPet.php/<?=$row["ID"]?>/"><?=$row["PetName"]?></A>

              There is no ID is sent to EditPet.php that why it generate an error

                Write a Reply...