Я пытаюсь разработать свой сайт локально, используя wampserver (последняя версия - 3.2.0). У меня были проблемы с этим, соблюдая ВСЕ мои правила в .htaccess. Оказывается, это не мои правила, я опубликовал в prod, и он нормально работает на размещенном сайте.
Так что проблема должна быть в wampserver. Я удостоверился, что mod_rewrite включен, и я могу подтвердить это, потому что мой глобальный маршрутизатор работает, это только мой маршрутизатор API, который не работает локально. Он обслуживает пустую страницу при запросе любого API. Интересно, что в случае сбоя ресурса мой маршрутизатор api будет вызываться:
Действителен, но не заполнен:
http://tastytracker/api/account/get
http://tastytracker/api/account/get.php
Неверный, правильная ошибка обслуживания:
http://tastytracker/api/account/gets.php
http://tastytracker/api/account/gets
Вот мой полный файл .htaccess :
# PHP Directives
php_value display_errors On
php_flag magic_quotes 1
php_flag magic_quotes_gpc 1
php_value mbstring.http_input auto
php_value date.timezone America/New_York
# Force https
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
# Friendly URLS
RewriteEngine On
RewriteBase /
# If the file or dir exists, serve it,
# otherwise send to api router
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^api/(.+)$ api/index.php [QSA,L]
# If the file or dir exists, serve it,
# otherwise send to router
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php [QSA,L]
Вот мой маршрутизатор API, расположенный в /api/index.php
:
<?php
// All my apis are the same template code
// so this page acts both as router and template
$request = strtolower(rtrim($_SERVER["REQUEST_URI"], "/"));
$arr = explode("?", $request, 2);
$GLOBALS["URIArr"] = $arr;
$request = $arr[0];
//$param = $arr[1]; // Unused but your able to get the query string here
// Build the path
$Path = dirname(__DIR__). $request . '.php';
// If path doesnt exist, switch to 404
if (!file_exists($Path)) {
http_response_code(404);
$return = ["message" => "Service not found.", "success" => false];
header('Content-Type: application/json');
echo json_encode($return);
return;
}
// Start of API Template
$return = ["message" => "", "success" => true];
try {
// Require the page
require $Path;
} catch (Exception $e) {
$return["message"] = $e->getMessage();
$return["success"] = false;
} finally {
header('Content-Type: application/json');
echo json_encode($return);
return;
}
Это не должно иметь большого значения, но чтобы заставить мой сайт работать, у меня установлен виртуальный хост вверх, иначе это не подкаталог в www.