.htaccess - 301 redirect if no query string present

流过昼夜 提交于 2019-12-04 19:56:51

You need to conditionalize the redirect and do it in PHP to prevent the infinite redirect loop.

index.php:

if(!isset($_GET['step'])){
    header('Location:http://www.monsite.com/index.php?step=1');
}

The way you have it configured will redirect indefinitely, since nothing is saying to the engine "don't redirect me as soon as the URL variable step is set".

There are ways to do this in the .htaccess file, but since these kind of redirects are generally application logic it seems to make more sense to do it directly in your script.

Or, for a pure .htaccess solution:

#if query string is empty

RewriteCond %{QUERY_STRING} ^$

#match on / for root, or .index.php in request and send to query string version 

RewriteRule ^(/|index.php)?$ /index.php?step=1 [R=301,L]
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!