Redirigir 301 a / si la URL contiene index.php

Mar 26 2020

Tengo un blog en laravel 5.8 y quiero redirigir 301 a / hay index.php en la URL.

Mi .htaccess es este

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

    RewriteEngine On

    # Handle www + https
    RewriteCond %{HTTPS} off [OR]
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

    # Redirect if index.php is in the URL
    RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
    RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]

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

He probado este enfoque:

    # Redirect if index.php is in the URL
    RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
    RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]

pero el problema es SI hay una URL como esta que contiene (? get_params)

htts://www.example.com/index.php?test

redirigirá 301 a /? test

¿Cómo puedo cambiar la regla para redirigir 301 a / (sin ningún parámetro?) Si la URL contiene index.php?

Respuestas

4 jeprubio Mar 26 2020 at 12:56

Puede utilizar el indicador QSD (Query String Discard) para redirigir sin pasar la cadena de consulta.

Cuando el URI solicitado contiene una cadena de consulta y el URI de destino no, el comportamiento predeterminado de RewriteRule es copiar esa cadena de consulta en el URI de destino. El uso del indicador [QSD] hace que se descarte la cadena de consulta.

# Redirect if index.php is in the URL
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteRule (.*?)index\.php /$1 [R=301,NE,L,QSD]