直接访问时重定向 index.php

redirect index.php when accessed directly

我试图设置我的 .htaccess 以重定向任何没有有效查询字符串直接进入我的 index.php 的流量。

当我说有效时,我的意思是:/index.php?page=home

因此,如果他们直接转到 index.php,它将重定向到 /

这是我的 .htaccess

options +FollowSymLinks
RewriteEngine on
RewriteBase /

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

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([/a-zA-Z0-9_\-]+)(/|)$ /index.php?page= [QSA,L]

我不确定这可能吗?

试试这个:

 RewriteRule ^index\.php / [NC,QSA,L]

应该这样做

我认为使用 Apache RewriteRule 无法匹配查询字符串(后面是什么?)

page.html?a=2&b=5

?a=2&b=5 部分无法与 Apache RewriteRule 匹配。您只能匹配 page.html 并且您可以选择 include/enable 通过使用 QSA 标志来查询字符串。否则查询字符串将被忽略。

重写规则 ^page.html 我的-page.php [QSA]

然后您可以使用 parse_url() 或 $_GET 从 PHP 读取查询字符串。

这将需要一个查询字符串并且请求的页面是''或'index.php'。如果是这样,它将重定向到 a.php。

options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{QUERY_STRING} ^$
RewriteCond %{REQUEST_FILENAME} ^(index.php|)$
RewriteRule .* a.php [R=301,L]

你应该可以做到这一点。检查是否存在任何查询,然后重定向到 root。那应该是最简单的形式。这是假设您的所有链接都有查询字符串。

RewriteCond %{QUERY_STRING} !.
RewriteRule ^index\.php$ / [R=301,L]