0

I have a question similar to this one:

I have created a small PHP program that I want to call as a rest web service to return some data from custom MySQL tables.

I can put it anywhere, but I've tried the root folder (public_html), a folder I creatd called custom, and the cgi-bin folder.

For the first two, I get 404 not found. For the CGI-BIN it looks like it redirects to my home page.

I've set it with CHMOD to 755 (and the custom folder as well).

My .htaccess looks like this. I think maybe it needs to change somehow?

# 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
1
  • Is there a particular reason you didn't use the WP REST API and $wpdb calls to fetch the data? I'm not sure what WP knowledge could be helpful here, this looks like a generic hosting issue that requires Apache htaccess knowledge
    – Tom J Nowell
    Commented Nov 14, 2017 at 18:11

2 Answers 2

1

Move Wordpress from root folder into subdirectory and create a .htaccess file in root folder, and put this content inside (just change example.com and my_subdir):

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteCond %{REQUEST_URI} !^/my_subdir/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /my_subdir/$1
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(/)?$ my_subdir/index.php [L] 

That's all :)

4
  • Thanks, about to try it. Is my_subdir where I put my PHP program, or where I moved WordPress? Commented Nov 14, 2017 at 18:18
  • Assuming my_subdir is wp, where I copied everything. Home page works, custom PHP still gives 404, and index.php (home page) works, but all other word press pages getting 404. E.g. The requested URL /wp/aboutcontact/ was not found on this server. Commented Nov 14, 2017 at 18:24
  • Plus I don't want to see /wp/ in the my URL or have existing pages change. Commented Nov 14, 2017 at 18:37
  • Couldn't I just add some line like this to handle the one PHP file: RewriteRule ^checkforms\.php$ custom/checkforms.php [R=301,L] (Tried, but still getting 404). Commented Nov 14, 2017 at 18:57
0

Okay, I think I was mislead by this... I didn't read the code thoroughly that I cloned. It sets a 404 if the SQL fails. Duh! I also discovered a file called /custom/error_log that I think is going to help debug that.

// die if SQL statement failed
if (!$result) {
  http_response_code(404);
  die(mysqli_error());
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.