javascript - 如何在 PHP 中将页面重定向到不带井号 (#) 的 URL?

标签 javascript php .htaccess redirect

前言:

我在以下地址:

http://www.example.com/#contact

成功提交联系表单后,用户将被重定向到以下地址:

http://www.example.com/thanks

借助以下代码:

// Redirect to thank you page
redirect_to("thanks");

函数redirect_to()定义为:

// Page redirection
function redirect_to($url)
{
    if(isset($url) && $url != '')
    {
        header("Location: " . $url);
        exit();
    }
}

感谢页面的htaccess规则是:

# Thanks Page
RewriteRule ^thanks/?$ thanks.php [QSA,L]
#RewriteRule ^thanks/$ thanks.php [QSA,L]

问题:

问题是哈希 (#) 字符,即片段部分仍然与 URL 保持一致,例如:

http://www.example.com/thanks#contact

该部分如何#contact如果我将页面重定向到thanks,请坚持使用主URL ?

我们如何通过 htaccess 或 PHP 甚至通过 JS 排除/删除它?

最佳答案

这个问题之前就被问过。

redirect is keeping hash

标记为答案的响应是:

The simple answer to "how do I stop it" is to specify an empty hash in the Location header:

header('Location: /account.html#');

However, this behavior isn't guaranteed across the board. It seems to work in WebKit and IE9 in my quick test. Nevertheless, you've stumbled on a black hole in the HTTP specification.

关于javascript - 如何在 PHP 中将页面重定向到不带井号 (#) 的 URL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45994017/

相关文章:

javascript - Curry 在 Sanctuary 和 Ramda 之间有何不同?

javascript - chrome 中的 DOM 更新比 firefox 慢看起来像 chrome 有一些渲染问题

javascript - extjs 中的 refs 是被覆盖还是被继承?

php - Web 服务器带宽是整个 HTTP 请求/响应吗?

php - Zend Paginator - 页面链接

javascript - 在特定 URL 目录 (htaccess) 上运行 javascript 函数

javascript - jquery 选择器向下钻取?

javascript - Angular - 将 HTTP 发布数据发送到服务器 [HOW TO]

.htaccess - 使用 .htaccess 从所有子目录中隐藏 index.php

.htaccess - 使用添加的URL参数进行重定向