All routes are also available under /index.php/whatever. This causes massive SEO duplicate content issues and could lead to unexpected behavior.
On these pages all links generated by the route helper get prefixed with index.php/ too.
A middleware with the redirect() helper function causes an infinite redirect loop:
if (Str::contains(request()->getRequestUri(), 'index.php/')) {
$url = str_replace('index.php/', '', request()->getRequestUri());
return redirect($url, 301);
}
Only a vanilla PHP header redirect is working currently for us:
if (Str::contains(request()->getRequestUri(), 'index.php/')) {
$url = str_replace('index.php/', '', request()->getRequestUri());
header("Location: $url", true, 301);
exit;
}
Example 1:
<option value="https://laravel.com/docs/5.8">5.8</option><option value="https://laravel.com/index.php/docs/5.8">5.8</option>Example 2:
I don't think this is an issue with Laravel itself, but rather the nature of PHP. Without rewrites URLs _without_ index.php would never work, because the webserver could never know where to look for the entrypoint, index.php.
Solving this would require additional webserver config adjustments.
Little trick with nginx
# Remove index.php$
if ($request_uri ~* "^(.*/)index\.php$") {
return 301 $1;
}
# Redirect index.php/*
if ($request_uri ~* "^(.*/)index\.php(/?)(.*)") {
return 301 $1$3;
}
Hi there,
Thanks for reporting but it looks like this is a question which can be asked on a support channel. Please only use this issue tracker for reporting bugs with the library itself. If you have a question on how to use functionality provided by this repo you can try one of the following channels:
However, this issue will not be locked and everyone is still free to discuss solutions to your problem!
Thanks.
@driesvints hm this might be something for Laravel to look into too, the laravel.com site suffers from this as well: https://laravel.com/index.php/docs/master (notice how index.php appears in the URL).
Like the original author mentioned, this will cause duplicate indexing since no canonical URL has been defined.
This appears to be the default for any Laravel project, perhaps a framework-level fix is in order?
I believe it's down to how nginx passes it to PHP. Laravel only explodes the segments after index.php so I'm not sure what Laravel could do with it.
@mattiasgeniar the reason why we can't force it through the framework is that, sometimes, shared hostings can't resolve pretty-printed urls and need the index.php in the url part. If you don't want it in the url part you'll need to configure redirects through your web server config like this: https://gist.github.com/slow-is-fast/68db4535780d6b46a8a8c777c1f2ba7a
(I also definitely see the pun in me linking web server configs to you 馃槀 )
Is it an idea to add this to the laravel server requirements documentation?
@robindirksen1 feel free to send in a PR
For others trying to solve this with Laravel, nginx of apache, see: https://ma.ttias.be/remove-index-php-from-the-url-in-laravel/