JayNic Posted July 13, 2017 Share Posted July 13, 2017 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 Quote Link to comment https://forums.phpfreaks.com/topic/304318-wordpress-site-htaccess-with-different-rules-in-subdirectory/ Share on other sites More sharing options...
requinix Posted July 14, 2017 Share Posted July 14, 2017 Make /api be an actual directory, first of all. Then you can basically reuse that in /api/.htaccess, but change the RewriteBase to suit. Quote Link to comment https://forums.phpfreaks.com/topic/304318-wordpress-site-htaccess-with-different-rules-in-subdirectory/#findComment-1548316 Share on other sites More sharing options...
JayNic Posted July 14, 2017 Author Share Posted July 14, 2017 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: 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: Quote Link to comment https://forums.phpfreaks.com/topic/304318-wordpress-site-htaccess-with-different-rules-in-subdirectory/#findComment-1548346 Share on other sites More sharing options...
requinix Posted July 14, 2017 Share Posted July 14, 2017 Name your file index.php. Unless you really don't want to? Quote Link to comment https://forums.phpfreaks.com/topic/304318-wordpress-site-htaccess-with-different-rules-in-subdirectory/#findComment-1548347 Share on other sites More sharing options...
JayNic Posted July 18, 2017 Author Share Posted July 18, 2017 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) Quote Link to comment https://forums.phpfreaks.com/topic/304318-wordpress-site-htaccess-with-different-rules-in-subdirectory/#findComment-1548550 Share on other sites More sharing options...
rx3mer Posted November 29, 2018 Share Posted November 29, 2018 @JayNic I am having a similar issue. Did you managed to figure it out? Quote Link to comment https://forums.phpfreaks.com/topic/304318-wordpress-site-htaccess-with-different-rules-in-subdirectory/#findComment-1562522 Share on other sites More sharing options...
requinix Posted November 29, 2018 Share Posted November 29, 2018 5 hours ago, rx3mer said: @JayNic I am having a similar issue. Did you managed to figure it out? Judging by the "I ended up getting it working", I'm going to say yes. Quote Link to comment https://forums.phpfreaks.com/topic/304318-wordpress-site-htaccess-with-different-rules-in-subdirectory/#findComment-1562528 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.