简体   繁体   中英

remove index.php from laravel route

I've a laravel 5.4 over apache centos server.

the folder structure are like: /var/www/html <- public folder /var/www/project <- laravel project folder /var/www/html contains the "public's laravel folder" content like css/ js/ and index.php etc.

index.php contains references to the project main folder. the project works but just with ip like:

myip/index.php/login

and not

myip/login

as I would like I try many way to remove it but without luck, this is my .htaccess file inside /var/www/html:

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

RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]

# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
RewriteRule ^(.*)$ index.php/$1 [L]


# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
 RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

Instead of changing the .htaccess file, you should change the server configuration to serve files from Laravel public folder.

Before editing the config file and to be safe, copy the default config file to have a backup: cp /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.original

Edit the file /etc/httpd/conf/httpd.conf as the follwoing:

change this line:

DocumentRoot "/var/www/html" 

to

DocumentRoot "/var/www/project/public"

Add the follwing lines to allow serving files from Laravel public folder:

<Directory "/var/www/project/public">
    AllowOverride All
    Require all granted
</Directory>

Run sudo apachectl configtest to make sure you did not make any mistake. You should get Syntax OK .

Restart the apache server to apply the new configuration. sudo systemctl restart httpd

Finally, restore the .htaccess file to the default version that comes with laravel at this link .

It's better to create a virtual host instead of changing the default configuration. However, this will be ok if you have only on website on the server.

You need to go on the laravel documentation deployement/apache and you will see the rewriter rules you need to put them rules in your apache configuration. And all will work

For those who work with laravel 7.x: MAke sure have .htaccess in both public and root folder of your project with bellow content

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

By the way, don't forget to set AllowOverride to All in /etc/httpd/conf/httpd.conf (on CentOS):

<Directory "/var/www/path/to/my/app/public">
    Options FollowSymLinks
    AllowOverride All
</Directory>

Goodluck.

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