Jump to content

Recommended Posts

Hi all,

 

I really don't understand htaccess very well, and I feel like what I'm trying to do is simple, but I can't get it to work.

 

I have a wordpress site, and in the public_html folder is the .htaccess file with the following body:

# Use PHP54 Single php.ini as default
#AddHandler application/x-httpd-php54s .php

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

That's all fine for the public website, but I'm trying to build a rest api in a subdirectory:

www.example.com/api/

 

- and I want all requests that go anywhere in /api to go to my single file: www.example.com/api/myapp.php

 then I plan on just handling everything with some php in there by parsing the path:

$path = strtolower(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
$path = rtrim($path, '/'); //remove trailing slash

// ... do stuff based on path

So for example, if I made a POST request to: 

https://www.example.com/api/v1/dothing

 - then myapp.php would find that $path is "/api/v1/dothing"

 

Then I can grab post params, get params what have you...

 

 

I don't want to impact the current wordpress site - ONLY the /api directory...

 

Thanks for your help

 

Thanks for the reply - I think I'm still not getting it right... cause now I still get the "index of /api" screen.

 

I have the following structure:

2017-07-14_1150.png

 

the and files:

.htaccess

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /api
RewriteRule ^api\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /api.php [L]
</IfModule>

api.php

<?php

header("Content-Type: application/json; charset=utf-8");

echo json_encode(["hey"=>"there"]);

but I'm still getting this:

2017-07-14_1152.png

Thanks for your help - I ended up getting it working with the following:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /api
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)$ index.php?request=$1 [QSA,NC,L]
</IfModule>

and in my /api/index.php file I can see everything:

//api/index.php
if (!array_key_exists('HTTP_ORIGIN', $_SERVER)) {
    $_SERVER['HTTP_ORIGIN'] = $_SERVER['SERVER_NAME'];
}


$echo = [
    "request" => $_REQUEST['request'], //The request directory with get params
    "getParams" => isset($_GET) ? $_GET : null,
    "postParams" => isset($_POST) ? $_POST : null,
    "fileInput" => file_get_contents('php://input'),
    "httpOrigin" => $_SERVER['HTTP_ORIGIN']
];

echo json_encode($echo)
  • 1 year later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.