I have a blog in laravel 5.8 and I want to redirect 301 to / it there is index.php in the url.
My .htaccess is this
<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>
I have tried this approach:
    # Redirect if index.php is in the URL
    RewriteCond %{THE_REQUEST} ^GET.*index.php [NC]
    RewriteRule (.*?)index.php/*(.*) /$1$2 [R=301,NE,L]
but the problem is IF there is an url like this one that contains (?get_params)
htts://www.example.com/index.php?test
it will redirect 301 to /?test
How can I change the rule to redirect 301 to / (without any ? params) if the url contains index.php ?
Advertisement
Answer
You can use QSD flag (Query String Discard) to redirect without passing the query string.
When the requested URI contains a query string, and the target URI does not, the default behavior of RewriteRule is to copy that query string to the target URI. Using the [QSD] flag causes the query string to be discarded.
# Redirect if index.php is in the URL
RewriteCond %{THE_REQUEST} ^GET.*index.php [NC]
RewriteRule (.*?)index.php /$1 [R=301,NE,L,QSD]