I want to achieve reading and printing the JSON data without .php URL extension. But the server is returning an error.
My PHP code (data.php) is as follows:
<?php
// GET FULL PAGE URL
$link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
// GET EVRYHING AFTER THE LAST SLASH
$params = substr($link, strrpos($link, '/') 1);
// PASS THE PARAMETERS TO API
$url = "http://api.plos.org/$params";
//read json file from url in php
$readJSONFile = file_get_contents($url);
header('Content-Type: application/json'); //
print_r($readJSONFile); // display contents
.htaccess file for .php extension removal
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
I'm getting the correct data if I access with the .php extension (https://local.test/data.php/search?q=title:DNA) when I was trying without the .php extension it was returning a 404 error. https://local.test/data/search?q=title:DNA
CodePudding user response:
You can try to change .htaccess
file content with:
RewriteEngine On
RewriteBase /
RewriteRule ^data/search$ data.php [L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)/?$ /$1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule . index.php [L]