我有一个页面index.php
,我想在用户的某个操作后将参数附加到URL的末尾。所以结果是index.php?param=1
使用下面的PHP代码导致无限重定向循环
<?php
header("Location: index.php?param=1");
?>
使用下面的Javascript代码会导致无限刷新循环。
window.location = "?param=1";
我怎么能追加参数只有一次没有遇到任何循环使用PHP, Javascript或jQuery?
对于PHP版本,尝试:
<?php
if( !isset( $_GET['param'] ) ){
header('location: index.php?param=1' );
}
?>
请尝试添加主机名,然后重试。
示例—如果您正在使用localhost.
<?php
header("Location: http://localhost/index.php?param=1");
?>
除了使用Apache的mod_rewrite之外,您还可以检查额外的参数是否已经设置,例如
<?php
if (isset($_SERVER['QUERY_STRING']))
header("Location: index.php?param=1");
?>
最好使用.htaccess
文件来设置重定向,因为有单独存储逻辑和服务器配置的良好做法。
.htaccess
代码:
Redirect /path/to/index.php http://yourdomain.com/path/to/index.php?param=1
或者如果您需要永久重定向,请使用
Redirect 301 /path/to/index.php http://yourdomain.com/path/to/index.php?param=1
如果你不使用Apache,而是使用Nginx或其他东西,请检查他们的文档如何进行重定向。