404 error with Slim - tried everything - php

I'm trying to build an API for the first time by using PHP to access a MySQL server, and going off of AndroidHive's tutorial here but customizing it to be my own simple API instead of the task manager one listed. My problem is, I can't even get to the part where I test anything (I'm using XAMPP for Mac) because I keep getting 404 errors when I attempt to access a method in my index.php file.
Here's my .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ %{ENV:BASE}index.php [QSA,L]
I already changed my XAMPP Apache httpd.conf file to "AllowOverride All" as per the Slim documentation instructions, and tried restarting the server a billion times but to no avail. No matter what I do, if I ever try to test any of the URLs in my index file (for instance at localhost/w_manager/v1/register) it just returns a 404 not found error.
I even updated my .htaccess file to add in "RewriteBase /v1" though that did not seem to make much of a difference - got another 404 object not found error.
I've also looked around on Stackoverflow a ton. I'm about to pull out my hair. Does anyone have any other suggestions? Thanks!

Well, I seem to have fixed it - not exactly sure how, unfortunately, but what I did was go back and copy the sample code into my own and make alterations (after I noticed that the sample code worked.) One major change I made was that in my index.php file, I moved the Slim request definitions at the beginning and moved all other functions to the end, just before $app->run(). Hope this will help someone else!

Related

server error using URI embedded images in a required file if index.php isn't in the url

Struggling. Help!
In index.php:
require "cachedPages/home.html";
If I visit: https://websiteaddress.org/index.php then it works fine.
If I visit: https://websiteaddress.org then I get an internal server error.
I guess it's a .htaccess thing. All I have in there is some cpanel php72 code and:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
DirectoryIndex index.php
It seems there's some difference between how index.php is called if you call it directly as opposed to .htaccess calling it for you?!?
Any ideas?
Thanks in advance.
If you don't have one of the following options in your .htaccess then Apache won't know which file to default back to:
DirectoryIndex index.php
FallBackResource /index.php
Also I'd recommend installing mod_rewrite as well. It's handy for other reasons.
Well, I've managed to sort the problem, but it's a pretty weird situation that no-one else will probably ever experience. Here goes...
If the user lands at:
https://websiteaddress.org rather than https://websiteaddress.org/index.php
and that page (php) requires another page,
which has images that are embedded as URIs rather than linked src files.
Then, the first URI causes a server error.
If I replace the
img src='data:image/jpeg;base64,/998a9g98ahg...etc'
with
img src='path/to/file.jpg'
on the first instance of a jpg then it all works fine.
All the later URIs are fine, it's just the first instance!
It all works now, with this workaround; and the situation is so unique and bizarre that I doubt this thread will be of use to anyone else. In fact it's so edge-case that I can't be bothered investigating it any further myself.

.HTACCESS RewriteRule not working as expected

I have a magento website, that is currently operating. Within a subfolder of this site, I have placed a 3rd party application that also has its own HTACCESS file to handle routing for its application.
When I access the folder http://example.com/somefolder the screen I expect shows up, but when I navigate to http://example.com/somefolder/newroute, I instead land on a magento 404 screen.
I have traced this to the magento htaccess file, in all cases, unlesss the path physically exists the rewriterule will always send the request to the index.php - this explains why Im getting there.
To fix this issue, I wrote a little rewriterule which I placed in the htaccess file of the magento store. The goal was to add an exception to any request that came through and contained any reference to my subfolder. The thought is now it should hit the path its supposed, then hit the htaccess file, and then route me to where IM supposed to be in this other application. Unfortunately it doesnt seem to work, after adding the rule I end up the same place - magento.
Here is what I've written:
RewriteRule ^(.*somefolder.*)$ $1 [L]
Im not sure what could be going wrong, as I think the approach seems pretty straight forward. Any ideas on how to remedy this situation?
Thanks.
Here is Your Simple Answer.Also Used By me on my site.
RewriteCond %{REQUEST_URI} !^/(yourfoldernameHERE)$

php file not accessible when url typed in to browser

I tried to search for this on the forums, but I don't know exactly what my issue is called. I hope this isn't redundant to another forum or a really stupid question. This is also my first post, so I read through what I need to put in this question, but please help me learn if I forgot to include something.
I FTPed a .php file over to my server into the /public_html folder which is where my index.php file are as well as everything else that I can get to show in my web browser (chrome). The file is named game-response-confirmation.php. I have the permissions for this file set to 777 on the server as well just for testing purposes (i'd like to set them back to 644 when I'm done if that doesn't have to do with the issue).
The issue is when I type in the url www.mywebaddress.com/game-response-confirmation.php into the address bar, I receive a 404 Not Found error. It says that the file was not found on this server.
To be honest, I'm not sure what other information you need to help troubleshoot this issue with me. Ask and I will provide more information.
For this, I would expect the page game-response-confirmation.php to appear as a webpage in the browser. Currently, the website is a simple echo since I needed to make sure it wasn't the php file that was creating the problem.
Thank you in advance for any help.
Look at ".htaccess" file. Perhaps there is wrong redirection inside.
For instance:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
Third line obtains all URLs and sends them to unknown place. In result you get error #404.
Golka had a great suggestion. going into the .htaccess file gives some information about what is going on. It told me, indirectly, that my host was switching over the server. So I was able to investigate and figure out that they are in the middle of the migration. Because of this, I didn't have the new server information and was uploading the .php page to the old server while the domain was pointed to the new server. So of course this file wasn't available! Silly. Thank you all for suggestions and help.

Restler always showing NOT FOUND on server, but not on local machine

When I go to
http://localhost/api/auth/login
I get what I expect.. some data that lets me log in.
However, when I go to
http://myurl.com/api/auth/login
I just get
{
"error": {
"code": 404,
"message": "Not Found"
}
}
Currently we have 2 people working on this project, and both of us, when we run it from our local machines, have no issues. We're to the point where we are putting the app on to a hosted server so that we can start testing it outside of our own machines.
The code is identical, and I know this because we use a git repository in which the server is also pulling from.
My machine is a mac, my buddy's is a windows machine, and our host is a linux box. This shouldn't really matter, since all of them should work with mod_rewrite, a requirement for Restler 3.
The only other details I can think of is that the server is hosted by HostMonster, running PHP 5.4.7
Any help would be GREATLY appreciated. Do you need more information?
=== Edit:
This is my .htaccess file. Also, the server runs CGI/FastCGI not mod_php
Options -MultiViews
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^$ index.php [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
<IfModule mod_php5.c>
php_flag display_errors Off
</IfModule>
I just googled 'fastcgi mod_rewrite' and none of those links helped :P
When ever you come across such issue first try without url rewriting
instead of
http://myurl.com/api/auth/login
try
http://myurl.com/api/index.php/auth/login
If it works you will know that the issue is with url rewriting
Since you are using CGI/FastCGI make sure you have set
cgi.fix_pathinfo=0
in your php.ini it will make sure that path info is passed to Restler properly so that restler can find the route
If it does not work with index.php in the url, you can try generating and verifying routes.php as explained below
routes.php
Restler looks at all api methods and its doc comments to generate routes accordingly every time we run it on debug mode. When we run it on production mode it captures the information in routes.php and uses it instead of generating routes every time thus improving efficiency.
You can check for the generated routes by initializing restler in production mode and refresh on every call
$r = new Restler(true, true);
and then check the generated routes.php
If your routes.php contains no routes it will appear as follows
<?php $o = array();
// ** THIS IS AN AUTO GENERATED FILE. DO NOT EDIT MANUALLY **
return $o;
If this is your case, it means some how autoloader is failing to load the api class, you can confirm it by manually including the API classes
If it starts to work, please file a bug using github issues stating that the autoloader is failing for your server configuration and give us more detils so that we can reproduce it and fix it.
If it still does not work, please file a bug using github issues mentioning your server configuration and give us any detail that could help so that we can reproduce it and fix it.
Thaks to you, we will also write a trouble shooting guide based on above to help the restler community :)

Mod-rewrite not working on Network Solutions server

I have a website that requires mod-rewrite to function well, but it seems to not be functional on the Network Solutions shared server we're running on. Network Solutions promises it's installed but won't provide any further support without additional payment.
I placed a simple test at the following folder which contains two files. The first, ".htaccess" contains the following text:
RewriteEngine On
RewriteRule ^link([^/]*).html$ rewrite.php?link=$1 [L]
The second is the PHP script which tests it, available here:
http://www.oceanhousefloridakeys.com/testmr/rewrite.php
All I see is that mod-rewrite is installed (no error messages showing) but the rewriteRule is not working. Can anybody see why this script isn't working... is there anything I can do to get it running, or is Network Solutions not telling the whole truth?
It looks like your script resides inside a sub-directory. In that case it's the best solution to set the RewriteBase to the correct path (before any RewriteRule):
RewriteEngine On
RewriteBase /testmr/
RewriteRule ^link([^/]*).html$ rewrite.php?link=$1 [L]

Categories