I'm trying to built some Rest api on Raspberry PI model B with Slim Framework. The problem is that when i run the / route all work fine, when i try to run /test route i have a 404.
I think there are some problem with the .htaccess and rewrite rule, this is my structure of the api:
var/www/html/ is the root
--api
--libs
--Slim
--index.php
--.htaccess
and this is my .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [PT,L]
and this is my index.php slim app:
<?php
require '/var/www/html/api/libs/Slim/Slim.php';
\Slim\Slim::registerAutoloader();
$app = new \Slim\Slim();
$app->get('/', function() use($app) {
echo "Root";
});
$app->get('/test', function() use($app) {
echo "test";
});
$app->run();
?>
Thanks in advance
Solution:
I've edited the file in etc/apache2/apache2.config changing these line
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None <----- this to All
Require all granted
</Directory>
then restarting apache2 with
sudo service apache2 restart
and i'll need also this line
sudo a2enmod rewrite
thanks #T0xicCode
Your apache configuration (check in /etc/apache2/) needs to have AllowOverride FileInfo or AllowOverride All for the directory where your files are (/var/www/html or /var/www/html/api). Otherwise apache will simply ignore the .htaccess and try to find a folder or a file named test. See the documentation.
Related
First I need to say ya sorry for my bad english
I hope u can help me strangers.
I have created a domian in my linux Debian 8.
look it
<VirtualHost *:80>
ServerAdmin admin#admin.com
DocumentRoot "/var/www/saci"
ServerName saci.com
ServerAlias www.saci.com
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<Directory "/var/www/saci">
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
I'm using slim and I've created a folder named 'app' on the folder 'saci' with a file start.php that got this:
<?php
use Slim\App;
session_cache_limiter(false);
session_start();
ini_set('display_errors', 'On');
define('INC_ROOT', dirname(__DIR__));
require INC_ROOT . '/vendor/autoload.php';
$app = new App();
$app->get('/test', function() {
echo "this is a test.";
});
?>
Now on my folder saci I've created a folder named Public with one index.php and one .htaccess.
so now look my roots
Var > www > saci > public
index.php <------------------index daaa
.htaccess <------------------.htaccess
var > www > saci > app
start.php <----------------start
so now my problem is here
I've got an index.php with this
<?php
require '../app/start.php';
$app->run();
and a .htaccess with this
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
but when I go to my host www.saci.com I can see my index of/
with all my proyects folders But I cannot see the folder public, and if I go to www.saci.com/public
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator at admin#admin.com to inform them of the time this error occurred, and the actions you performed just before this error.
More information about this error may be available in the server error log.
Apache/2.4.10 (Debian) Server at www.saci.com Port 80
I rly DON'T HAVE IDEA of what is going wrong I need rly rly help
starting now
Thanks Steangers U rock never forget smile
Greetings from Venezuela
This is my folder structure for Laravel 5.2
-laravel
--app
--bootstrap
--config
...
Under the laravel folder that is root of Laravel, I added an .htaccess file with following code:
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
Now when I open project in browser I get the error:
Sorry, the page you are looking for could not be found. "NotFoundHttpException in RouteCollection.php line 161:"
I am using XAMPP. Why I am getting this error? How can I remove public from the URL using this method?
You need to point your web server to a public directory instead of project root directory. Use original .htaccess and these Apache settings:
DocumentRoot "C:/xampp/htdocs/public"
<Directory "C:/xampp/htdocs/public">
Restart Apache to make it work.
The best way is to create a virtual host in xampp and set its DocumentRoot to laravel public folder.
Here is a nice tutorial on how to create vhosts in xampp
https://delanomaloney.com/2013/07/10/how-to-set-up-virtual-hosts-using-xampp/
create a folder and name as you want, move all the files inside that folder except public, move the content of public folder to root. After you must edit index.php and change [newFolder] by your news folders name
require __DIR__.'/[newFolder]/bootstrap/autoload.php';
$app = require_once __DIR__.'/[newFolder]/bootstrap/app.php';
•Go to mainproject/public>>
a. .htacess
b. favicon.ico
c. index.php
d. robots.txt
e. web.config
1.cut these 5 files from public folder,and then paste on the main project folder that’s means out side of public folder… mainproject/files
2.Next after paste ,open index.php ,modify
•require __DIR__.'/…/bootstrap/autoload.php'; to
•require __DIR__.'/bootstrap/autoload.php';
modify
$app = require_once DIR.'/../bootstrap/app.php'; to
$app = require_once DIR.'/bootstrap/app.php';
Change your httpd.conf file from
DocumentRoot "C:/xampp/htdocs"
<Directory "C:/xampp/htdocs">
to
DocumentRoot "C:/xampp/htdocs/{projectname}/public"
<Directory "C:/xampp/htdocs/{projectname}/public">
restart apache. then you can now access your project in url localhost/
I'm new to this, I have a problem with following code:
<?php
require_once 'vendor/autoload.php';
require_once 'lib/controller/Root.php';
require_once 'lib/controller/General.php';
$app = new \Slim\Slim(array(
'debug' => true,
));
$root_controller = new Root();
$general_controller = new General();
$app->map('/', function() use ($app, $root_controller) {
$root_controller->index();
})->via('GET', 'POST');
$app->map('/vorstand', function() use ($app, $general_controller) {
$general_controller->vorstand();
})->via('GET', 'POST');
$app->map('/satzung', function() use ($app, $general_controller) {
$general_controller->satzung();
})->via('GET', 'POST');
$app->run();
?>
I'm using the slim framework and I can't call the routes localhost/vorstand or localhost/satzung everytime I get "Not found" And I have no explanation for that :/ only localhost which means the route "/" works.
For Apache server, two things need to be check.
1.The .htaccess file in the directory (same directory with your index.php) contains:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
2.You also need a directory directive to enable .htaccess files and allow the RewriteEngine directive to be used. This is sometimes done globally in the httpd.conf file, but its generally a good idea to limit the directive to just your virtual host by enclosing it in your VirtualHost configuration block. This is generally setup in your configuration in the form of:
<VirtualHost *:80>
ServerAdmin me#mysite.com
DocumentRoot "/path/www.mysite.com/public_html"
ServerName mysite.com
ServerAlias www.mysite.com
#ErrorLog "logs/mysite.com-error.log"
#CustomLog "logs/mysite.com-access.log" combined
<Directory "/path/www.mysite.com/public_html">
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
Add AllowOverride All to your DocumentRoot Directory.
Following the documentation, Silex allows "slugs" to be passed in via the URL for use within your code.
The following example works:
$app = new Silex\Application();
$app->get('/', function () {
return 'HAI';
});
However, the following gives a 404 Not Found:
$app = new Silex\Application();
$app->get('/{slug}', function ($slug) {
return 'HAI' . $slug;
});
How can I fix this 404?
In case it's of any relevance, here's my Apache Vhost:
<VirtualHost 127.0.0.1:80>
DocumentRoot "/var/www/Silex/web"
DirectoryIndex index.php
<Directory "/var/www/Silex/web">
AllowOverride All
Allow from All
</Directory>
</VirtualHost>
...and my directory structure:
/src
|-- bootstrap.php
/tests
/vendor
/web
|-- index.php
It turns out that this was an Apache issue. It was assumed that you could either use a .htaccess file, or a vhost. You actually need to use both.
.htaccess:
FallbackResource /index.php
Note: You can only use FallbackResource if using Apache 2.2.16 or higher.
vhost
<VirtualHost 127.0.0.1:80>
DocumentRoot "/var/www/Silex/web"
DirectoryIndex index.php
<Directory "/var/www/Silex/web">
AllowOverride All
Allow from All
</Directory>
</VirtualHost>
An alternative is to place the contents of the .htaccess file (the FallbackResource directive) within the vhost itself, and get rid of the htaccess.
As soon as I added the .htaccess, the slug in my second example worked.
So i'm running lampp on ubuntu 11.10 and i have the following problem
i need this url:
localhost/myname/oefeningenPHP/cms/index.php?head=about
rewritten to:
localhost/myname/oefeningenPHP/cms/about
The url is kinda messy because i have lots of folder with different PHP exercises for school.
My .htacces is located in:
localhost/myname/oefeningenPHP/cms
since cms is my sites root.
.htacces contains the following code:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^/([a-z]+) /index.php?head=$1
</IfModule>
But returns a 404 error.
Furthermore i'm sure the rewrite module is being loaded since it's uncommented in the http.conf file
I've ran out of ideas so i'm hoping someone here can help me. I'm not very sure about the forward slashes in the RewriteRule, but i've tried various things and nothing works
try
RewriteRule ^([a-z]+) index.php?head=$1 [L]
You should also see if you .htaccess is enabled by adding some junk to it e.g. asdfasdfasdfasd, save it and try to access the URL. If you do not get a 500 error, then it is not enabled.
To enable it look for a line that says
AllowOverride None
and change it to
AllowOverride All
If you have already run sudo a2enmod rewrite command but your URL Rewrite is not working.
To make .htaccess files work as expected, you need to edit this file:
Step1 : sudo vi /etc/apache2/sites-available/default
Step2 : Look for a section that looks like this:
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
# Uncomment this directive is you want to see apache2's
# default start page (in /apache2-default) when you go to /
#RedirectMatch ^/$ /apache2-default/
</Directory>
You need to modify the line containing AllowOverride None to read AllowOverride All.
step 3 : sudo /etc/init.d/apache2 reload