NotFoundHttpException Lumen in production - php

When access Api, return print below:
Symfony\Component\HttpKernel\Exception
NotFoundHttpException
enter image description here
the project is in "back" folder and the url is api/
Root .htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^api(/(.*))?$ back/public/$1 [L]
RewriteRule ^(.*) $1.php [NC,L]
.htaccess in public
Options -MultiViews -Indexes
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

For this problem, I think you need to add a request capture inside the service container of the app.
For that, You need to add this code inside your public/index.php
$request = IlluminateHttpRequest::capture();
$app->run($request);
Or you can do one other solution by putting this code inside same file
$app->run($app->make('request'));

Related

laravel project serve not showing welcome page

I have started working on laravel CodeIgniter was a piece of cake in terms of development but laravel is pretty much full of stress I have installed laravel project using artisan command
compose create-project –-prefer-dist laravel/laravel micamp
After successfully installing i created an .htaccess file to serve laravel to web browser instead of
https://localhost/micamp/public
to
https://localhost/micamp
.htaccess which I created
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
Placed the .htaccess on the main root directly nwo when I serve laravel project by typing in to the browser localhost/micampp
I see 404 not found instead of laravel welcome page
if you want to remove public follow the below steps ,
rename server.php (micamp/server.php) to index.php
paste below code in .htaccess file (micamp/.htaccess)
Options -MultiViews -Indexes
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Options -MultiViews -Indexes
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.gif|robots\.txt)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ public/$1
RewriteRule ^(css|js|images|trainer-cv)/(.*)$ public/$1/$2 [L,NC]
php artisan cache:clear
php artisan config:cache
then try now it will work

How to check non index directory using htaccess

I want to redirect to the main domain if a directory(any) exists
but does not have an index file(index.php/index.html), How to do that? I tried the below code but does not work.
Options +FollowSymlinks -Indexes
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ %1 [R=301,L]
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{DOCUMENT_ROOT}/$1/index.php !-f [OR]
RewriteCond %{DOCUMENT_ROOT}/$1/index.html !-f
RewriteRule ^(.+)/$ / [L,R]
This will redirect a directory request to / if the /index file doesn't exist in that folder.

How to remove .php extension the pro way

I have created my first Php solution but have trouble removing the .php extension from the URL. It works in the root but not in sub-folders?
Eg www.domain.com/index.php reached from www.domain.com/index (or www.domain.com/)
Then it gets tricky.
For example. www.domain.com/en/europe/bikes/racer-bike.php will not differ from www.domain.com/en/bikes/racer-bike without ending up on www.domain.com/en/bikes/ and on index.php
My .htaccess file looks like this:
Options -MultiViews
RewriteEngine on
RewriteBase /
# Remove .php-extension from url
RewriteCond %{REQUEST_URI} ^/(.*).php$
RewriteRule ^(.*)$ %1 [L,QSA]
# Add trailing slash to url
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/|#(.*))$
RewriteRule ^(.*)$ $1/ [R=301,L]
# SSL
RewriteCond %{HTTP_HOST} ^domain.com$
RewriteRule ^(.*)$ http://www.domain.com$1 [R=301]
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^/?(.*)$ https://%{SERVER_NAME}/$1 [L,R=301,QSA]
I Try it and it works:
My dirs structure
/
index.php
/sub
file.php
And my htaccess like this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/([^\/]*)$
RewriteRule ^([^\/]*)$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ([A-Za-z]+)/(.*)$ [NC]
RewriteCond %{DOCUMENT_ROOT}/%1 -d
RewriteRule ^([A-Za-z]+)/(.*)$ $1/$2.php [NC,L,QSA]
Try it
You are doing it wrong ... Mostly you do not make php files for each uri (route). You should not remove php extension you should direct all uri's to index.php and use some router to define how to handle each request. or you can just use some framework to do that like laravel or symfony etc.

Send all request to v1/index.php - htaccess

I'm in the process of building an api and want to redirect all requests to v1/index.php
My folder structure is;
-htdocs
--v1
----index.php
This is my htaccess atm
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ /index.php [NC,L]
I want all requests sent to api.example.co.uk to go to v1/index.php
I want all requests sent to api.example.co.uk to go to v1/index.php
You can have this rule in root .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} =api.example.co.uk
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ v1/index.php [L]
If you point you domain to /htdocs/v1/ folder and place inside with your index.php this .htaccess file it will redirect any request to index.php
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ /index.php [L]

Modification to a RewriteRule

The following RewriteRule works correctly
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://domain.com/$1/ [L,R=301]
until I place this one just right below
RewriteRule ^([a-z0-9]+)/$ /profile/company-profile.php?cname=$1 [NC,L]
Now every domain.com/something-here goes to the company-profile.php
How can I fix this ?
Firstly, the domain.com/login takes you to the index.php of the login folder.
When I type the second RewriteRule, if I write domain.com/login again it shows (in the background) the company-profile.php?cname=login
I would recommend to use this:
RewriteEngine On
RewriteBase /
# redirect company specific request
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z0-9\-_]+)/?$ profile/company-profile.php?cname=$1 [NC,L]
# redirect all others
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_URI} !/?$
RewriteRule ^(.*)$ http://domain.com/$1/ [L,R=301]

Categories