Appropriate use of .htaccess

1

I have a server with a web application developed in laravel, I am trying to configure the .htaccess file so that, if a URL is entered with index.php , it disappears.

I am currently using these lines:

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

This makes:

  • if the URL comes as midominio.com/index.php/home
  • becomes midominio.com/home

The problem (and consequently an error) is:

  • when a URL like this comes: midominio.com/index.php
  • transforms the url to: midominio.com/var/www/Proyecto/public

If I use this in the .htaccess

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

This last error is corrected, but it does not replace the index.php of the URL. If I put the two solutions is fixed, but I would like to know if there is a solution that brings these two alternatives together.

Examples:

I need the URL how are you

midominio.com/index.php/loQueSea
midominio.com/index.php

Modify to:

midominio.com/loQueSea

.htaccess complete

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

    RewriteEngine On
    RewriteBase /

    # 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]

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

    #Se saca el index.php
    #RewriteCond %{THE_REQUEST} ^.*/index\.php
    RewriteRule ^(.+/)?index\.php(?:/(.*))?$ $1$2 [R=301,NC,L]

</IfModule>
    
asked by Ignaxox 29.06.2017 в 18:15
source

1 answer

3

I do not see the point of using a RewriteCond in this case.

We can simply redirect by capturing everything before and after index.php .

RewriteEngine on

RewriteRule ^(.+/)?index\.php(?:/(.*))?$ $1$2 [R=301,NC,L]


Description:

  • ^ - Matches the beginning of the text.

  • (.+/)? - Optional group. Matches any text ending in / . Capture in $1 .

  • index\.php - Literal index.php .

  • (?:/(.*))? - Optional group. Matches:

    • / - Literal.
    • (.*) - Matches any text. Capture in $2 . Does not include the / initial in the capture.
  • $ - Matches the end of the text.

  • [R=301] - Redirect 301.

  • [NC] - Ignoising uppercase / lowercase.
  • [L] - If it matches, do not process more rules.


Demo:

I uploaded the .htaccess to a free hosting to show a live demo:

I created the /82546/loQueSea folder. Any other test will work, but obviously it will give a 404 error because the resource does not exist.

    
answered by 29.06.2017 / 19:03
source