doujianqin5172 2017-06-03 00:22
浏览 39
已采纳

使用Amazon EC2,我仍然可以使用Rest.inc.php(REST API),还是有不同的方法?

I'm trying to migrate my REST APIs for a project to Amazon AWS. I have PHP 7.0 installed and working on an EC2 instance. However, I cannot seem to get my API routing working at all. The original version uses Rest.inc.php and .htaccess file. I've set this up on several other servers without any problem, but can't get it working under Amazon AWS.

What I'm wondering is where the problem lies. I'm just getting a not found error on the API calls. Is this likely related to security/signing issues, or should I be using a completely different approach on AWS?

The code I'm using is very similar to the example that seems to popup everywhere you search.

<?php

    require_once("Rest.inc.php");

    class API extends REST {

        public $data = "";

        const DB_USER       = "username";
        const DB_PASSWORD   = "password";

        private $db = NULL;

        public function __construct(){
            parent::__construct();
            $this->dbConnect();
        }

        private function dbConnect(){
            try {
                $this->db = new PDO("mysql:host=myAWShost;dbname=database", self::DB_USER, self::DB_PASSWORD);
                $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            }
            catch(PDOException $e) {
                echo "Connection failed: " . $e->getMessage();
            }
        }

        public function processApi(){

            $func = strtolower(trim(str_replace("/","",$_REQUEST['rquest'])));

            if((int)method_exists($this,$func) > 0) {
                $this->$func();
            } else {
                $this->response('',404);
            }

        }

        private function activity(){
            ...
        }

        private function appointment(){
            ...
        }

        private function client(){
            ...
        }

        private function facility(){
            ...
        }

        private function person(){  
            ...
        }

        private function personLocation(){
            ...         
        }
    }

    // Initiiate Library

    $api = new API;
    $api->processApi();

?>

.htaccess file:

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-s
    RewriteRule ^(.*)$ api.php?rquest=$1 [QSA,NC,L]

    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^(.*)$ api.php [QSA,NC,L]

    RewriteCond %{REQUEST_FILENAME} -s
    RewriteRule ^(.*)$ api.php [QSA,NC,L]   
</IfModule>
  • 写回答

1条回答 默认 最新

  • dongxiqian2787 2017-06-03 01:17
    关注

    For Apache to read the .htaccess file you need to ensure that AllowOverride All is set within your Apache config file, and then restart apache with sudo service httpd restart.

    Example:

    <Directory "/var/www/html">
    AllowOverride None
    </Directory>
    

    Becomes:

    <Directory "/var/www/html">
    AllowOverride All
    </Directory>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装