Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Hi i am trying to get the request method when the URL is requested from my localhost. I have done URL rewriting so that http://localhost/API2/products.php?product_id=1 becomes http://localhost/API2/products.php/products/1 as a result $request_method=$_SERVER["REQUEST_METHOD"]; should hold GET as the method. But rather it gets zero every time.

my.htaccess file holds these url rules:

      RewriteEngine On # Turn on the rewriting engine
      RewriteRule ^products/?$ products.php [NC,L]
      RewriteRule ^products/([0-9]+)/?$ products.php?product_id=$1 [NC,L]

My curl commands in learn.php are:

   <?php
    $url = 'http://localhost/API2/products';
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_HTTPGET, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response_json = curl_exec($ch);
    curl_close($ch);
    $response=json_decode($response_json, true);

   ?>

So when i run my curl command which is learn.php neither it presents me any data because $request_method=$_SERVER["REQUEST_METHOD"] the request method is holding zero value.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
204 views
Welcome To Ask or Share your Answers For Others

1 Answer

Add that to disable MultiViews:

Options -MultiViews

The Apache docs on mod_negotiation, describes what the Multiviews Option does, when enabled:

If the server receives a request for /some/dir/foo and /some/dir/foo does not exist, then the server reads the directory looking for all files named foo.*, and effectively fakes up a type map which names all those files, assigning them the same media types and content-encodings it would have if the client had asked for one of them by name. It then chooses the best match to the client's requirements, and returns that document.

Use:

# disable products -> products.php
Options -MultiViews
# Turn on the rewriting engine
RewriteEngine On
RewriteRule ^products/?$ products.php [NC,L]
RewriteRule ^products/([0-9]+)/?$ products.php?product_id=$1 [NC,L]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...