Backbone.js save gets '404 not found' on Slim.php - .htaccess

I have created a Backbone.js model and want to save an instance to my MySql database.
var Website = Backbone.Model.extend({
defaults: {
"title":"default title"
},
urlRoot : './websites'
});
var website = new Website();
website.save();​
I am using Slim.php to create a Restful API to my database. Here is the beginning of websites\index.php:
<?php
require 'Slim/Slim.php';
$app = new Slim();
$app->get('/websites', 'getWebsites');
$app->get('/websites/:id', 'getWebsite');
$app->post('/websites', 'addWebsite');
$app->put('/websites/:id', 'updateWebsite');
$app->delete('/websites/:id', 'deleteWebsite');
$app->run();
My save() triggers a POST which gets "moved permanently":
Request
URL:localhost/SAMPLE-CODES/backbone.js-mysql-reading-json/websites
Request Method:POST Status Code:301 Moved Permanently
Then I see a second http request sent:
Request
URL:localhost/SAMPLE-CODES/backbone.js-mysql-reading-json/websites/
Request Method:GET Status Code:404 Not Found
My question is: why is this request not triggering the call to the 'addWebsite' function ?
I see that the second http request is a GET, when it should be a POST, but even then there is a route for that...
I have a folder /websites/
I also set the the .htaccess and http.conf as per the Slim routing documentation:
.htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
http.conf
<VirtualHost *:80>
<Directory "c:/xampp/htdocs/SAMPLE-CODES/backbone.js-mysql-reading-json/websites/">
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
Edit:
The call to model.save() triggers an http POST to
localhost/SAMPLE-CODES/backbone-mysql-reading-json/websites
.
This gets a '301 Moved Permanently' with response header says:
localhost/SAMPLE-CODES/backbone-mysql-reading-json/websites/
.
I assume this is due to the Slim recommended .htaccess settings.
Question: Is this OK to have this '301 Moved Permanently', or do I already have an issue here?
Then I see 2nd http GET to
localhost/SAMPLE-CODES/backbone-mysql-reading-json/websites/
Update:
I am still getting a 301 on
localhost/SAMPLE-CODES/backbone-mysql-reading-json/websites
, then a GET to
localhost/SAMPLE-CODES/backbone-mysql-reading-json/websites/
This second request works on a browser and returns
[{"id":"1","title":"titre site 1"},{"id":"2","title":"titre site 2"}]
So that would be the response for all records, but I wanted to save 1 record.
It seems the redirect 301 is wrong.
I have a feeling it is due to Slim not finding a matching route (Slim_Exception_RequestSlash would trigger a 301).
But why my Slim script not find the route?
The request for
localhost/SAMPLE-CODES/backbone-mysql-reading-json/websites
is matched by:
$app->post('/websites', function() {

I also use SlimPHP and have a setup just like yours.
What PHP are you using? If you're using a version >= 5.3 then the way you're defining your routes would be incorrect. For a newer PHP, try this:
$app->get('/websites', function () {
// your code to execute
});
Everything else about your setup looks pretty normal to me.

Problem solved thanks to orangewarp and this question:
Why do I get a 301 redirect to folder name with slash?
Solution was to add DirectorySlash Off in the .htaccess:
My updated .htaccess (where the app calls Slim.php):
RewriteEngine On
DirectorySlash Off
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]

Related

How to remove public? from url in php project

I am currently working to build a small php mvc framework. in a framework i have a this folder structure.
-app
--controllers
-Post.php
-core
-logs
-public
--.htaccess
-- index.php
-vendor
in here index.php is working as Front Controller
in post controller is look like this..
<?php
/**
* Posts controller
*
*/
class Posts
{
public function index()
{
echo 'Hello index';
}
public function addNew()
{
echo 'Hello addNew';
}
}
in url, i want to remove project/public/?posts/index public/?. When i remove (public/?) and visit the url. its showing me this error message.
project/posts/index
The requested URL was not found on this server.
using public/? project/public/?posts/index is working fine. and its echo index message
project/public/
The .htaccess inside of the public folder contains:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
in project main root folder ...
i did't added .htaccess and index.php file.
in .htaccess when i add this line. url redirect to xammp welcome screen
RewriteRule ^(.*)$ index.php?$1 [L,QSA]
I'd say you want to internally rewrite all incoming requests to the controller inside the /project/public folder. But that is not what you do. The rule you implemented (RewriteRule ^(.*)$ index.php?$1 [L,QSA]) only rewrites relative to the requested folder. No mentioning of "public" in there.
The actual setup you need depends a bit on your http host setup here. Where its DOCUMENT_ROOT points to. Most likely to the folder that contains the file system structure you posted in your question. If so you should implement a rule that rewrites all incoming requests to the /project/public folder.
Something like that, though you probably need to tweak it to match your actual setup:
RewriteEngine on
RewriteRule ^ /public/index.php?%{REQUEST_URI} [L,QSA]
You can implement such rule in the http server's host configuration. Or, if you do not have access to that, you can use a distributed configuration file (if you have enabled those for the http host), so a ".htaccess" style file. That file should be located inside the folder your http hosts DOCUMENT_ROOT setting points to. So the folder containing the file system structure your posted.
Other setups are possible, this is just one option. The point is: you need to rewrite the requests to your controller. Where the controller actually is.

mywebsite.com work but www.mywebsite.com don't work well !! opencart

My Website (opencart) works great on mywebsite.com but i got problems when trying www.mywebsite.com,
eg: icons on header and products disappear.
I tried to add a line on config.php:
<?php
// HTTP
define('HTTP_SERVER','http://mywebsite.com/');
// HTTPS
define('HTTPS_SERVER', 'http://mywebsite.com//public_html/');
// DIR
define('DIR_APPLICATION', '/home/XXXX/public_html/catalog/');
define('DIR_SYSTEM', '/home/XXXX/public_html/system/');
define('DIR_DATABASE', '/home/XXXX/public_html/system/database/');
define('DIR_LANGUAGE', '/home/XXXX/public_html/language/');
define('DIR_TEMPLATE', '/home/XXXX/public_html/view/template/');
define('DIR_CONFIG', '/home/XXXX/public_html/system/config/');
define('DIR_IMAGE', '/home/XXXX/public_html/image/');
define('DIR_CACHE', '/home/XXXX/public_html/system/cache/');
define('DIR_DOWNLOAD', '/home/XXXX/public_html/download/');
define('DIR_LOGS', '/home/XXXX/public_html/system/logs/');
// DB
define('DB_DRIVER', 'mysql');
define('DB_HOSTNAME', 'localhost');
define('DB_USERNAME', 'XXXX_com1');
define('DB_PASSWORD', 'XXXXXXXXX');
define('DB_DATABASE', 'XXXX_com1');
define('DB_PREFIX', 'oc_');?>
define('HTTP_SERVER','http://www.mywebsite.com/');
under
define('HTTP_SERVER','http://mywebsite.com/');
nothing happened.
i'm beginner on opencart, How can I solve this ?
Opencart only works over 1 url,
You have never been a opencart website working with two different url, to fix that your will have to include the following in your htaccess.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^yourDomain\.com
RewriteRule (.*) http://yourDomain.com/$1 [R=301,L]
This code redirect, all urls with www to urls without www.
This fix your problem.
The source

How redirect specific url using htaccess rewrite rules

How can I redirect this URL:
http://domain.com/index.html#!
To this One:
http://domain.com/
Using htaccess rewrite rules?
For /index.html to / you need:
RewriteEngine On
RewriteCond %{THE_REQUEST} \ /+index\.html
RewriteRule ^ / [R=301,L]
That could incidentally fix the #! part of your URL as well, but that part of the URL is called a fragment, and is added there by something that's on the client side, it stays on the client side, and it is never transmitted to the server. So no htaccess or server side anything is going to know it's even there. If you want to remove it, you need to figure out what script is adding it there and remove the script. If you simply add javacsript to remove it, that other script may inadvertently add it back.
if (location.href.indexOf("#") > -1) {
location.assign(location.href.replace(/\/?#/, "/"));
}

Yii 404 page - override parent htaccess

I'm using the Yii framework at my company site.
I have a frontend at domain.com/... and a Yii based app at domain.com/app/.. .
We recently added a 404 page to the frontend site, with htaccess's ErrorDocument handler at the frontend. Problem is, the Yii app inherits from that 404, which gives a bad page. How can I force it to use Yii's embedded error handling page?
Thanks in advance.
There are 2 good article talking bout this:
How to use default layout for error pages instead of errorXXX views
Handling errors using an action
The idea is, you create a action in out main Controller that will handle all page errors, so, you can change your htaccess to redirect your application to the Controller:
domain.com/site/error
Ended up using a RewriteCond redirection instead of ErrorDocument, resulting in a "rule" for the file to be redirected. Basically something like this:
RewriteCond %{REQUEST_URI} !^/app
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* /404.php
Basically this means, "if file requested is not valid file, use this rewriterule". The first condition is to make sure it excludes the "app" folder, so it handles its own errors.

.htaccess RewriterRule - redirecting to index of site

I've got the following Rewrite:
RewriteEngine On
Options +Indexes
RewriteRule ^/#/why-us/44534/$ http://www.new-site.com/why-us/? [R=301,NE,NC,L]
RewriteRule ^/#/about-us/33945/$ http://www.new-site.com/about-us/? [R=301,NE,NC,L]
I've put this in the .htaccess file in the root of my site. However, when visiting:
www.old-site.com/#/why-us/44534 I just get the 'Index of /' page for old-site.com
Not sure why the url structure of old site has /#/ at the start, but it's not something I can control!
It is because browser doesn't send any request to web server for URL part after hash sign that's why for a URL of: www.old-site.com/#/why-us/44534 browser just sends www.old-site.com/ to web server while trying to scroll down page to an undefined tag why-us/44534.

Resources