0

Having used Route::get('/', ContnameController@methodName, how can I make Laravel (5.8 and above) show its default 404 page when myapp/index.php is used in the address bar? In another word: How can I prevent Laravel from showing a file with .php extension when an alternative Route is used to access the same file?

Apache Mod Rewrite is enabled and there is no problems related to it. Everything related to it works fine and all requests are directed to index.php as expected. I only do not want the user to use myapp/index.phpinstead of what I define in Route::get('/',.... Laravel seems to treat both interchangeably!

This is not a duplicate of Apache Mod rewrite question. Now the default (out of the box) Laravel home page is pulled from welcome.blade.php and is accessed via laravelapp/ as per the default Route::get('/', ... But that same page is also accessible via laravelapp/index.php. Is that an expected and accepted behavior if you do not want extensions in your app URLs? Obviously, if this is undesired behavior, then the Apache Mod rewrite is not the culprit even if there is an .htaccess fix.

4
  • You need to enable rewrite. stackoverflow.com/questions/12448912/… Commented Nov 27, 2019 at 13:16
  • It is enabled and works fine in all other cases that relate to rewrite mode. Commented Nov 27, 2019 at 13:59
  • Possible duplicate of Apache Mod Rewrite For Laravel Commented Nov 27, 2019 at 14:01
  • It has nothing to do with Apache Mod Rewrite. Please do not rush to link an issue to another without enough understanding of the difference. Commented Nov 27, 2019 at 14:05

2 Answers 2

1

One way is to use a rewrite rule, an apache example would be:

RewriteCond %{REQUEST_URI} index.php$
RewriteRule ^(.*)index.php$ $1 [R=404]

Another way is to add something like below in one of your service providers:

namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
    // ...
    public function boot()
    {
        if (Str::endsWith(Arr::get($_SERVER, 'REQUEST_URI', ''), 'index.php')) {
           abort(404);
        }
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for the response. The first suggestion resulted in showing Apache default Not Found error page. The second got Whoops! to throw FatalThrowableError (E_ERROR) Class 'App\Providers\Str' not found. Is is possible to configure it to show Laravel default 404 page?
Str needs use Illuminate\Support\Str; at the top of the file after the namespace. Arr may also need use Illuminate\Support\Arr;
YES! Now it works as I wanted it! Thank you so much.
0

write this in your public\.htaccess file just after RewriteEngine On line

RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php[^/] /$1? [L,R=302,NC,NE]

RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php(?:/(.*))?$ /$1$2? [L,R=302,NC,NE]

3 Comments

Thank you so much for the response. This resulted in omitting the index.php from the address bar, but redirected the request to the home page. What I try to do is getting it to show the Laravel 404 default (or custom) page. Is that possible?
Yes it is possible but in this case you have to redirecte to another url where you can call you custom error template
I tried hard to come up with a way to do something like that before I bother good people with questions, but I just couldn't do it the correct way. If possible, please give me the steps. Grateful for the responses.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.