简体   繁体   中英

Laravel how to remove index.php from url

I've created an about page in Laravel and pointed it to routes, but the problem is when I go to the URL: http://localhost:8080/bsms/public/about it doesn't work, and it says

Sorry, the page you are looking for could not be found.

However, http://localhost:8080/bsms/public/index.php/about is working fine. I want to my URL free from index!

These are my routes 在此输入图像描述

Are you in localhost?

Try adding this line to your .htaccess file in your public folder:

RewriteBase /

So it should be like this:

<IfModule mod_rewrite.c>
RewriteBase /
<IfModule mod_negotiation.c>
    Options -MultiViews -Indexes
</IfModule>

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>

If it doesn't work, try to give some more details about your project environment.

Also, url should be pointing to something like this: bsms.test or similar.

You should be able to access your about page through this url http://localhost:8080/bsms/about (taking in consideration that your view files are in resources folder and not in public folder).

Give it a try using these comands

php artisan config:clear
php artisan cache:clear
composer dump-autoload

And let us know how it goes.

go to /public/index.php and add blow code after define('LARAVEL_START', microtime(true));

/*
 * redirecting addresses with /index.php/
 * */
$url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
if (strpos($url, '/index.php')) {
    $url = str_replace('/index.php', '', $url);
    header('Location: ' . $url);
    die();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM