The requested URL was not found on this server error - php

I'm trying to create a website with Twig and Silex but I get a 404 Not Found error when I want access to /test page.
http://127.0.0.1/public_html/M1-CSI/web/ works.
http://127.0.0.1/public_html/M1-CSI/web/test doesn't works.
.htaccess
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
</IfModule>
Routing file
<?php
$routes = $app['controllers_factory'];
$routes->get('/', 'Sources\\Controllers\\ExempleController::exemple');
$routes->get('/test', 'Sources\\Controllers\\ExempleController::test');
return $routes;
Controller
<?php
namespace Sources\Controllers;
use \Silex\Application;
class ExempleController {
public function exemple(Application $app) {
return $app['twig']->render('exemple.html.twig', array('exempleValue' => 3));
}
public function test(Application $app) {
return $app['twig']->render('test.html.twig');
}
}
Thank's a lot.
EDIT
You can find source code extract here : https://github.com/nlamblin/website-boilerplate
SOLUTION
Problem solved.
I added these code lines :
<Directory /var/www/html/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
in /etc/apache2/sites-available/000-default.conf

Related

Laravel 8.53 - Routing doesnt work on any of pages

Solution
The solution was found thanks to #Gert B.
Simply add to your Laravel application any virtual host (I added for mine)
laravel1.test
How to add Virtual host:
Go to C:\Windows\System32\drivers\etc\hosts
add line:
127.0.0.1 laravel1.test (or your virtual host name)
And add this to your vhosts(in case of using xampp) in C:\xampp\apache\conf\extra httpd-vhosts
<VirtualHost *:80>
ServerName www.laravel1.test
ServerAlias laravel1.test
DocumentRoot "C:\xampp\htdocs\Laravel1\public"
<Directory "C:\xampp\htdocs\Laravel1\public">
# use mod_rewrite for pretty URL support
RewriteEngine on
# If a directory or a file exists, use the request directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Otherwise forward the request to index.php
RewriteRule . index.php
# use index.php as index file
DirectoryIndex index.php
# ...other settings...
# Apache 2.4
Require all granted
## Apache 2.2
# Order allow,deny
# Allow from all
</Directory>
</VirtualHost>
I can't pass the problem with routing in Laravel 8.5 framework. I know there have been many requests about this problem so far but any of given solutions didnt help in my case. I will show Frontend code, since backend is the same thing and also doesnt work. Right now, the only thing that works is index.php.
I know thanks to newest Laravel all bugs are hidden under simple error 404.
Bug image
I already tried:
Changing in apache/conf/httpd solution AllowOverride All
Clearing whole cache and routes cache
Any possible misspells in code
Still, without any working anwser.
My FrontendController at app/Http/Controllers
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class FrontendController extends Controller
{
public function index()
{
return view('frontend.index');
}
public function object()
{
return view('frontend.object');
}
public function article()
{
return view('frontend.article');
}
public function person()
{
return view('frontend.person');
}
public function room()
{
return view('frontend.room');
}
public function roomSearch()
{
return view('frontend.roomsearch');
}
}
My web.php at app/routes
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\FrontendController;
##Frontend routes
Route::get('/','FrontendController#index')->name('index');
Route::get('/object','FrontendController#object')->name('object');
Route::get('/article','FrontendController#article')->name('article');
Route::get('/person','FrontendController#person')->name('person');
Route::get('/room','FrontendController#room')->name('room');
Route::get('/roomSearch','FrontendController#roomsearch')->name('roomSearch');
##Backend routes
Route::group(['prefix'=>'admin'],function(){
Route::get('/','BackendController#index')->name('adminHome');
Route::get('/cities','BackendController#cities')->name('cities');
Route::get('/myObjects','BackendController#myobjects')->name('myObjects');
Route::get('/profile','BackendController#profile')->name('profile');
Route::get('/saveObject','BackendController#saveobject')->name('saveObject');
Route::get('/saveRoom','BackendController#saveroom')->name('saveRoom');
});
.htacces file in app/public
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
Options +FollowSymlinks
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
My routes
image
Solution
The solution was found thanks to #Gert B.
Simply add to your Laravel application any virtual host (I added for mine) laravel1.test
How to add Virtual host: Go to C:\Windows\System32\drivers\etc\hosts
add line:
127.0.0.1 laravel1.test (or your virtual host name)
And add this to your vhosts(in case of using xampp) in C:\xampp\apache\conf\extra httpd-vhosts
<VirtualHost *:80>
ServerName www.laravel1.test
ServerAlias laravel1.test
DocumentRoot "C:\xampp\htdocs\Laravel1\public"
<Directory "C:\xampp\htdocs\Laravel1\public">
# use mod_rewrite for pretty URL support
RewriteEngine on
# If a directory or a file exists, use the request directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Otherwise forward the request to index.php
RewriteRule . index.php
# use index.php as index file
DirectoryIndex index.php
# ...other settings...
# Apache 2.4
Require all granted
## Apache 2.2
# Order allow,deny
# Allow from all
</Directory>
</VirtualHost>

Controller function not found on the server in codeigniter

So I got exactly this 404 error :
Not Found
The requested URL /adminigniter1/Usercontroller/insert was not found on this server.
Apache/2.4.18 (Ubuntu) Server at localhost Port 80
When I wanted to call a function from the controller, from what I read about it has to do with the .htaccess file(and I placed that in my source folder adminigniter1), but mine seems to look ok:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /adminigniter1/index.php
</IfModule>
From my index.php I send to that controller function like this:
<form method = "POST" action = "<?php echo base_url('Usercontroller/insert') ?>">
And this is my controller :
<?php
class Usercontroller extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('Usermodel');
}
public function index() {
$data["content"]= "user/index";
$data["getStatus"] = $this->Usermodel->getStatus();
$this->load->view("main",$data);
}
public function insert() {
$datai= $this->input->post();
if(isset($datai)){
echo $datai['txtApartament'];
exit;
}
}
}
What can be wrong ?
Ok, I found the reason for that, I will post it here since some one can have this problem. All the issues were on apache2 side:
If you use ubuntu go :
sudo gedit /etc/apache2/apache2.conf
There you will have to edit field like this :
<Directory /var/www/html/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
In my case AllowOverride was set to None, so you will have to set it to All, another thing, check the root for your localhost /var/www/html/ in my case, to be ok!

unable to remove index.php from laravel url ?? in ubuntu 14.04

there
I just created a new project in laravel 5.2. then I had successfully removed public/index.php from url of my project in windows it works fine.
But While I copied the same project in ubuntu inside var/www/html as /var/www/html/myproject. And enabled apache2 rewrite_mode. But it no work without putting index.php in url. It works without public only.
http:://localhost/myproject/index.php/cms (it works fine) BUT
http:://localhost/myproject/cms(it does not works)??? .Any suggestion????
my code is as
route.php
define( "PREFIX", "cms" );
Route::get(PREFIX . '/login', 'Auth\AuthController#getLogin');
it fine
also I already cut all files from public folder to root directory and edit index.php file as
require __DIR__.'/../bootstrap/autoload.php';
By
require __DIR__.'/bootstrap/autoload.php';
and
$app = require_once __DIR__.'/../bootstrap/app.php';
by
$app = require_once __DIR__.'/bootstrap/app.php';
.htaccess as
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
I ran into this and foud a solution on http://laravel.io.
On Apache 2.4.7 the apache2.conf is set to not allow .htaccess override by setting AllowOverride None:
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
You can change this in apache2.conf, but this is not recommended. Instead do this override in the VirtualHosts file for your site ie. sites-available/yoursite.com.conf. Something like this:
<Directory /var/www/html/yoursite.com/laravelappinstall/public/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
Make sure that you have enabled the rewrite module ie. sudo a2enmod rewrite. Restart your web server ie. sudo service apache2 restart.
Source: http://laravel.io/forum/09-15-2015-removing-indexphp-from-url-laravel-5116
•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';

Slim route with parameter not working

I'm trying to setup Slim for me application i've got issue that route with parameters not working.
This is my index.php
require 'Slim/Slim.php';
\Slim\Slim::registerAutoloader();
$app = new \Slim\Slim();
// GET route
$app->get('/', function () {
print "hello";
});
$app->get('/books/:id', function ($id) {
print $id;
});
$app->run();
This is the .htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
Request to / works fine, but if I pass /books/1 it returns [404] not found
Man your mod_rewrite is not enable in apache.For enable mod_rewrite follow this.Reference http://www.kingpabel.com/apache-mod-rewrite/
<Directory /var/www/html>
AllowOverride All
</Directory>
//enable mod rewrite
a2enmod rewrite
//restart apache
service apache2 restart

Explain: Apache Rewrite, public in URL

I realize there are many hits for public in URL however I would like an explanation.
Apache configuration:
<VirtualHost *:80>
ServerName localhost
ServerAdmin vagrant#localhost
DocumentRoot /sites/MVC/public
<Directory /sites/MVC>
Options +FollowSymLinks +MultiViews
AllowOverride All
Require all granted
</Directory>
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
public/.htaccess:
Options -MultiViews -Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*) /index.php?url=$1 [QSA,L]
Basically when I try to print_r() the url received, it prints public as the first element of the array. The URL http://127.0.0.1:8080/this/that will result in this output:
Array ( [0] => public [1] => this [2] => that )
Why is it that public is included in the url when it is not explicitly stated?
Edit
P.S. For anyone wondering; the reason 127.0.0.0.1:8080 resolves to port 80 is because it is being hosted via a VM managed by Vagrant.
index.php creates an instance of App which breaks up the URL.
App.php:
class App {
// Defaults. This will default the app to home/index with 0 parameters
protected $controller = "home";
protected $method = "index";
protected $params = [];
public function __construct() {
print_r($this->parseUrl());
}
private function parseUrl() {
if(isset($_GET['url'])) {
return $url=explode('/', filter_var(rtrim($_GET['url'], '/'), FILTER_SANITIZE_URL));
}
}
}
I had the .htaccess file in the wrong directory, it should have been in the public/ directory rather than the / root directory of the application itself. Very junior mistake I know..
I hope this helps anyone else making such a simple mistake.

Categories