如果301重定向没有查询字符串present - 的.htaccess [英] .htaccess - 301 redirect if no query string present

查看:164
本文介绍了如果301重定向没有查询字符串present - 的.htaccess的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从页面创建一个301相同的页面,只是在目标页面中出现的URL的参数。浏览器告诉我一个错误(过多的重定向),所以它似乎有一个无限循环。这是我的code:

i would like to create a 301 from a page to the same page except that the destination page has a parameter in the url. Browsers show me an error (too many redirects) so its seems that there is an infinite loop. This is my code:

RewriteEngine on
Redirect 301 /index.php http://www.monsite.com/index.php?step=1

感谢指点我为:D

thanks to advice me for that :D

推荐答案

您需要条件化重定向并做到这一点在PHP prevent无限重定向循环。

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

的index.php:

index.php:

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

您有它配置将无限期地重定向的方式,因为没有什么是说给发动机不尽快URL变步设置重定向我。

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

有办法做到这一点在.htaccess文件中,但由于这类重定向通常是应用程序逻辑似乎更有意义,直接在脚本中做到这一点。

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.

或者,对于一个纯粹的.htaccess解决方案:

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]

这篇关于如果301重定向没有查询字符串present - 的.htaccess的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆