URLにindex.phpが含まれている場合は、301を/にリダイレクトします

Mar 26 2020

私はlaravel5.8にブログを持っていて、301をリダイレクトしたい/ URLにindex.phpがあります。

私の.htaccessはこれです

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

私はこのアプローチを試しました:

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

しかし問題は、(?get_params)を含むこのようなURLがある場合です

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

301を/?testにリダイレクトします

URLにindex.phpが含まれている場合、301を/(?paramsなし)にリダイレクトするようにルールを変更するにはどうすればよいですか?

回答

4 jeprubio Mar 26 2020 at 12:56

あなたは使用することができますQSDフラグクエリ文字列を渡さず、リダイレクトする(クエリ文字列破棄を)。

要求されたURIにクエリ文字列が含まれ、ターゲットURIに含まれていない場合、RewriteRuleのデフォルトの動作は、そのクエリ文字列をターゲットURIにコピーすることです。[QSD]フラグを使用すると、クエリ文字列が破棄されます。

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