Cannot remove public/index.php from url after changing configuration and .htaccess file

363 Views Asked by At

I am working on my local server right now and trying to get rid of '/public/index.php' from http://localhost/example/public/index.php/ in the url. I have followed content from the site: https://www.tutsmake.com/how-to-remove-public-from-url-in-laravel/ and added the config file laravelex.conf with the following content:

<VirtualHost *:80>
    ServerName localhost
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/example/public

    <Directory /var/www/example>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

In my project, uder the public folder I have .htaccess file as follows:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^(.*)$ public/$1 [L]
</IfModule> 

But when I'm trying to open http://localhost/laravel_project_folder/ or even http://localhost/laravel_project_folder/index.php/ I'm getting Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator at webmaster@localhost to inform them of the time this error occurred, and the actions you performed just before this error.

More information about this error may be available in the server error log.
Apache/2.4.29 (Ubuntu) Server at localhost Port 80

The error in the log file is

AH00124: Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.

Note: I have also tried to change .htaccess in the following way by looking at the answer by @stephen:

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
2

There are 2 best solutions below

2
On

change the file structure like this:

├── project folder
│   ├── core
│   │   └── laravel files
│   ├── html
│   ├── css
│   └── index.php

copy all of the project files to the core folder and copy the content of the public folder to the main folder.

then fix path of autoload.php and app.php in index.php:

require __DIR__.'/core/vendor/autoload.php';

$app = require_once __DIR__.'/core/bootstrap/app.php';
0
On

Sorry for the late reply first. But I found that I eventually had an answer from different sources. First I put my project files in the following order :

├──.cpanel/
├── public_html/
    ├── .htaccess
    ├── index.php
    ├── web.config ...
├── public_ftp/
├── laravel_project_folder
│   ├── app
│   ├── bootstrap
│   ├── config ...
│   └── index.php

Now I have changed file content in two places.

  1. Changed the index.php in the following lines :

require DIR.'/../vendor/autoload.php'; and $app = require_once DIR.'/../bootstrap/app.php';

to

require DIR.'/../laravel_project_folder/vendor/autoload.php'; and $app = require_once DIR.'/../laravel_project_folder/bootstrap

/app.php';

  1. changed the content of .htaccess too.