1

I am trying to build a simple RESTful API using PHP. The request works with a normal query in the URL path, however, I am getting a "404 URL not found" error when implementing REST approach

.htaccess

Options +FollowSymlinks
RewriteEngine On
RewriteRule ^([a-zA-Z|_-]*)$ index.php?name=$1 [nc,qsa]

index.php (snippet)

// process client request (via URL)
header("Content-Type:application/json");
include('functions.php');
if(!empty($_GET['name'])){

    $name=$_GET['name'];
    $result=get_items_by_category("$name");

    if(empty($result)){
      print($name);
      deliver_response(200,"Duh, gee, Mort, there's nothing here!",NULL);

      }else{
      deliver_response(200,"Hit the road,Jack!",$result);
      }

  }else{

  deliver_response(400,"This is not the request you're looking for",NULL);

}
7
  • 1
    What is the URL you are sending that is causing 404? Commented Apr 22, 2016 at 9:50
  • I would prefer using slim framework Commented Apr 22, 2016 at 9:57
  • 1
    I did some more debugging, and I found out that /api/Grill(my) does not work, maybe due to parentheses in the name? Because /api/Pasta works Commented Apr 22, 2016 at 10:00
  • 2
    There are no parentheses in the regex so yeah it won't match that. What is your plan for URL syntax? can help you with a regex to get it to work. Commented Apr 22, 2016 at 10:01
  • 1
    turned that into an answer, as i don't think you'll get a better one unfortunately without more information - if you need any more help drop it in comments and I'll update my answer for you. Commented Apr 22, 2016 at 10:13

1 Answer 1

1

Quote:

I did some more debugging, and I found out that /api/Grill(my) does not work, 
maybe due to parentheses in the name? Because /api/Pasta works

There are no parentheses in the regex so yeah it won't match that.

What is your plan for URL syntax? can help you with a regex to get it to work.

Sign up to request clarification or add additional context in comments.

3 Comments

well, right now I am trying to get the regex to give me multiple variables: RewriteRule ^additem/(.*)/*(.*)/*(.*)/*(.*)/*(.*)/*(.*)/*(.*)/*(.*)/*(.*)/*$ additem.php?name=$1&desc=$2&price=$3&isVeg=$4&isHot=$5&isNuts=$6&isGluten=$7&thumb=$8&cat=$9 Is there a way to get this working, or better alternative to this?
the . character will match everything, including forward slashes which is going to mess up how it reads the url if anyone puts one in, or possibly just the /'s that are in there anyway. I would specify actual character sets for each section of the URL, otherwise no there's not really a nicer alternative to it afaik
Yepp, ended up using ([0-9A-Za-z]+) for strings and ([0-9]+) for numbers, worked like a charm. My bad for not consulting any reading material beforehand.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.