redirect确切的url

我在.htaccess中使用以下指令将example.com/script-dir/index.php(与www和不带)redirect到example.com/index.php。 但它也redirectexample.com/script-dir/index.php?page=faq和其他网页。

RewriteEngine on RewriteCond %{HTTP_HOST} ^example\.com$ [OR] RewriteCond %{HTTP_HOST} ^www\.example\.com$ RewriteRule ^script\-dir\/index\.php$ "http\:\/\/example\.com\/" [R=301,L] 

我应该怎么做只redirect单个页面?

查询string( ?page=faq )不是在RewriteRule中testing的URL的一部分,所以你必须添加一个单独的条件来testing它:

 RewriteEngine on RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ RewriteCond %{QUERY_STRING} ^$ RewriteRule ^script-dir/index\.php$ http://example.com/ [R=301,L] 

我不确定为什么其他页面会被redirect。

您可以基于空的QUERY_STRING重写方块,按照与其他答案相同的方法: https : //stackoverflow.com/a/7591367/4363163 。 此外,您可能更喜欢精确匹配,而不依赖于RegExp,如其他答案中所述: https : //stackoverflow.com/a/24476841/4363163