Explain: Apache Rewrite, public in URL - php

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.

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>

PHP Slim4 on Apache2 gives HttpNotFoundException

Links I've seen include:
https://akrabat.com/running-slim-4-in-a-subdirectory/
https://discourse.slimframework.com/t/slim-4-httpnotfoundexception/3273/18
and some others
I'm trying to setup a Slim4 project on apache2. But trying to access it gives me the error
PHP Fatal error: Uncaught Slim\Exception\HttpNotFoundException: Not found....
I have my index file in /var/www/html/_projects/work/calc1/src.
The following is my calc.conf in /etc/apache2/sites-available:
<VirtualHost *:80>
ServerName calc.mrid.local
ServerAlias calc.mrid.local
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html/_projects/work/calc1/src
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
This is my .htaccess located in src directory
RewriteEngine On
RewriteBase /_projects/work/calc1/src/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]
RewriteRule ^(.*)\.html$ /$1 [L,R=301]
My index.php looks like this:
// use and require statements...
$container = new Container();
AppFactory::setContainer($container);
$container->set('view', function() {
return \Slim\Views\Twig::create(__DIR__ . '/public/templates');
});
$app = AppFactory::create();
// even without this line it doesn't work
$app->setBasePath("/_projects/work/calc1/src");
$app->add(TwigMiddleware::createFromContainer($app));
// my routes....
$app->run();
Has anyone worked with Slim4 on Apache2? If yes, can you please guide me ?
The src/ directory is actually only meant for the PHP classes and must not be accessible to the web. The apache DocumentRoot should point to the public/ directory which contains your front controller public/index.php. Take a look at the Standard PHP package skeleton. More details about Slim and apache can be found in this Slim 4 Tutorial.

PHP and Redirect with apache2.conf

I have an internal domain like
live.domain.com
which I defined in sites-available like so
ServerName live.domain.com
ServerAdmin webmaster#localhost
DocumentRoot /data/www/html/mydomainnamenodots
<Directory /data/www/html/mydomainnamenodots>
Require all granted
</Directory>
and the accordingly same for SSL conf.
I do have a folder /api which is in there like
/data/www/html/mydomainnamenodots/api
What I want is to catch all calls to
live.domain.com/api/*
and redirect them to something like
live.domain.com/api/index.php?url=*
So a real example would be
live.domain.com/api/statistics/getFTE/123/456
and I'd like that to be
live.domain.com/api/index.php?url=/statistics/getFTE/123/456
That would result in a $_REQUEST in index.php like so
array(1) {["url"]=>string(33) "/statistics/getFTE/123/456"}
mod_rewrite is enabled and loaded. Simple tests with file to file redirects work.
My rewrite rule which does not work as it does not redirect is
<Directory "/data/www/html/mydomainnamenodots/api">
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*?mydomainnamenodots\/api(.*)$ /mydomainnamenodots/api/index.php?url=$1 [QSA,L]
</Directory>
Any ideas why that does not work?
Am I missing the RewriteBase or something?
Thanks in advance
Alex
As the subdomain already points to a subfolder, it has to be the the according subfolder below that one in the directory definition. In addition I changed the regexp as well.
Here is what worked for me:
<Directory />
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*\/api(\/.*)$ /api/index.php?url=\/$1 [QSA,L]
</Directory>

Can't access rest api method when Codeigniter app in sub folder

I have problem accessing my REST api methods when I put my Codeigniter app in subfolder. I have placed Codeigniter app inside rest folder. Then I try to call http://myapp.dev/rest/api/Workingday/workingDay and I get 404 not found, same problem appears if I try to visit http://myapp.dev/rest/index.php
This is my folder structure
And this is my extenden rest api folder, so you can see that I have api folder in controllers and there will be public and rest APIs.
this is my .htaccess
RewriteEngine on
#RewriteCond $1 !^(index\.php|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /rest/index.php/$1 [L]
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]
And this is my vhost on Apache server (localhost development)
NameVirtualHost *:80
<VirtualHost *:80>
ServerName myapp.dev
ServerAlias *.myapp.dev
ServerAdmin info#myapp.dev
DocumentRoot "/Users/user/workspace/myapp" LogLevel debug
ErrorLog "/Users/user/workspace/myapp/rest/application/logs/myapp.dev-error_log"
CustomLog "/Users/user/workspace/myapp/rest/application/logs/myapp.dev-access_log" common
</VirtualHost>
This is my WorkingDay api in case you see any problem here, maybe... And location of file is /Users//workspace/myapp/rest/application/controllers/api/Workingday.php
class Workingday extends REST_Controller {
public function workingDays_get($id=NULL){
if($id != NULL){
$condition['id_working_day'] = $id;
$workingDays = $this->Working_days->get($condition);
}else{
$workingDays = $this->Working_days->get();
}
if($workingDays){
$this->response($workingDays, REST_Controller::HTTP_OK);
}else{
$this->response([
'status' => FALSE,
'message' => 'No time of day data were found'
], REST_Controller::HTTP_NOT_FOUND);
}
}
}
I think that the problem is in .htaccess file, but I just can't figure out how to properly configure it. How can I properly configure everything I need in order to be available to develop my application further?
****UPDATE**
I think that visiting http://myapp.dev/rest/index.php is working, because its showing Codeigniters 404 page (there is just none initial controller initialized)
access log:
127.0.0.1 - - [09/Dec/2017:17:58:00 +0100] "GET /rest/ HTTP/1.1" 404 1130
127.0.0.1 - - [09/Dec/2017:18:00:54 +0100] "GET /rest/index.php HTTP/1.1" 404 1130
while visiting http://myapp.dev/rest/api/Workingday/workingDay shows browsers 404 page
access log
127.0.0.1 - - [09/Dec/2017:17:53:18 +0100] "GET /rest/api/Workingday/workingDay HTTP/1.1" 404 228
I don't know what exactly i did, but some how my application sudenly started working...
This is my .htaccess file
RewriteEngine on
#RewriteCond $1 !^(index\.php|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /rest/index.php/$1 [L]
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]
This is my vhost conf (I think that solved the issue)
<VirtualHost *:80>
ServerName myApp.dev
ServerAlias *.myApp.dev
ServerAdmin info#myApp.dev
DocumentRoot "/Users/<user>/workspace/myApp"
LogLevel debug
ErrorLog "/Users/<user>/workspace/myApp/rest/application/logs/myApp.dev-error_log"
CustomLog "/Users/<user>/workspace/myApp/rest/application/logs/myApp.dev-access_log" common
<Directory "/Users/<user>/workspace/myApp">
Options Indexes FollowSymLinks
AllowOverride All
Allow from all
</Directory>
</VirtualHost>

The requested URL was not found on this server error

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

Categories