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

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>

Related

Why Apache is authenticating but not serving AUTHENTICATE variables in some routes?

I have a Virtual Host in a WAMP server. To acces that VH, you need to authenticate to a LDAP server, so I configured it like this:
<VirtualHost *:80>
ServerName myserver
DocumentRoot /apps/portal/public
<Directory "/apps">
Options FollowSymLinks
AllowOverride None
AuthType Basic
AuthBasicProvider ldap
AuthName "My Autenticacion"
AuthLDAPURL "ldaps://myldapserver:636/DC=DOM1,DC=COM?sAMAccountName,memberOf?sub?(objectClass=*)"
AuthLDAPBindDN myuser
AuthLDAPBindPassword mysecret
LDAPReferrals Off
Require valid-user
</Directory>
# My Laravel 7 app:
Alias /myapp /apps/myapp/public
<Directory "/apps/myapp/public">
# Laravel rewrites:
RewriteEngine On
RewriteBase /myapp/
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</Directory>
</VirtualHost>
So, all applications are under the /apps local directory. You can access the host via http://myserver. You can access the Laravel 7 application via http://myserver/myapp.
In that application I read the contents of the PHP variable $_SERVER['AUTHENTICATE_MEMBEROF']. When I visit http://myserver/myapp everything goes fine. But let's say I define one route like this:
Route::get('about', function() {
return view('about');
})->name('about');
Really simple. But as soon as I go to http://myserver/myapp/about, I get this error:
Undefined index: AUTHENTICATE_MEMBEROF
Furthermore, I've realized that none of the AUTHENTICATE_* server variables are served in this page. However, if I want to access http://myserver/myapp/about, the server prompts for credentials. So it's authenticating the user, but not serving the AUTHENTICATE_* variables.
What am I missing?
EDIT: Well, finally, I've found out that in all these routes, all AUTHENTICATE_* variables are available through a different name, which is REDIRECT_AUTHENTICATE_*, which doesn't seem to be documented anywhere. I assume this has to do with the rewrites, but anyone could explain why this new names, and whether it is possible or not to avoid these renamings?

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>

htaccess invisibly rewrite folder

I have multiple-domains with different content on one server in the directory /html/, which serves all domains, like example1.com, example2.com, example3.com, example4.com.
This works fine.
I now need to have 2 different(!) wordpress-installations. From the outside I would like to have:
example1.com -> No Blog
example2.com/blog/
example3.com -> No Blog
example4.com/blog/
The internal path-structure on the server should look like:
/html/
/blog-for-server2/
/blog-for-server4/
Because otherwise the content-management-system in /html/ gets messed up. In fact this is almost like having independent subdomains for the blogs and pointing them to different pathes on the server (but I don't want to use subdomains).
Any ideas?
Assuming you've already created the domains in Apache's httpd-vhosts.conf file you can create the redirects inside that, something like:
<VirtualHost *:80>
ServerName example1.com
DocumentRoot /home/servers/common_root/html
RewriteEngine On
RewriteRule ^/blog/? /blog-for-server-1
</VirtualHost>
<VirtualHost *:80>
ServerName example2.com
DocumentRoot /home/servers/common_root/html
RewriteEngine On
RewriteRule ^/blog/? /blog-for-server-2
</VirtualHost>
Otherwise you can do it in a root .htaccess but it's messy:
RewriteEngine On
RewriteCond %{SERVER_NAME} =example1.com
RewriteRule ^blog /blog-for-server-1
RewriteCond %{SERVER_NAME} =example2.com
RewriteRule ^blog /blog-for-server-2

Categories