ConfusedClown
ConfusedClown

Reputation: 1

Removing index.php from url with .htaccess file. The code is in laravel framework

Normally the index.php in laravel framework is in the /public_html/public/ folder. In my case it is in /public_html folder

This is the code inside my .htaccess file

RewriteEngine On

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

# Handle Front Controller...

Options -MultiViews
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

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

What changment should I make to exclude index.php from urls ?

The code structure is as follows :

/public_html/sitemap.xml

/public_html/sitemap.htm

/public_html/server.php

/public_html/robots.txt

/public_html/readme.md

/public_html/index.php

/public_html/.htaccess

/public_html/public

/public_html/database

/public_html/config

/public_html/cgi-bin

/public_html/bootstrap

/public_html/app


and inside /public_html/public


/public_html/public/web.config

/public_html/public/robots.txt

/public_html/public/favicon.ico

/public_html/public/upload

/public_html/public/js

/public_html/public/img

/public_html/public/css

Upvotes: 0

Views: 1122

Answers (3)

Sandeep Yadav
Sandeep Yadav

Reputation: 587

After spending hours I write below code for me and its 100% working.

Use this code just after

RewriteEngine On

Redirect index.php to non index.php

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

Upvotes: 0

Paul Basenko
Paul Basenko

Reputation: 1015

I use this Laravel project's structure too (index.php in the project's folder). In my case I did next:

  1. rename server.php in the root folder to index.php
  2. Make .htaccess in the root folder:

Insert this code inside the IfModule mod_rewrite.c

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]

RewriteCond %{REQUEST_URI} !(\.css|\.ttf|\.woff|\.woff2|\.js|\.png|\.jpg|\.gif|robots\.txt)$ [NC]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

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

RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(css|js|images|fonts)/(.*)$ public/$1/$2 [L,NC]

Upvotes: 1

Gayan
Gayan

Reputation: 3704

I highly discourage moving index.php out from the public folder. I was able to achieve what you are trying to do by following this tutorial.

There, you move your code base out from public_html folder to /appications/your-app folder and move content of public folder into public_html/your-app.

The tutorial is self-explanatory.

Upvotes: 1

Related Questions