简体   繁体   English

如何在PHP中使用#结尾检测,删除和重定向URL?

[英]How do I detect, remove, and redirect urls with a # at the end in PHP?

Client asked me to automatically redirect any urls with a pound sign in them to a version without a pound sign, but I don't think I am detecting a # in any of my urls with this formula. 客户要求我自动将任何带有英镑符号的网址重定向到没有英镑符号的版本,但我不认为我在使用此公式的任何网址中检测到#。 I emailed myself a sample URL using the curPageURL() formula and it didn't contain the trailing # sign in the url I was testing. 我使用curPageURL()公式通过电子邮件向自己发送了一个示例网址,它在我测试的网址中没有包含尾随的#符号。

function curPageURL() {
 $pageURL = 'http';
 if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
 $pageURL .= "://";
 if ($_SERVER["SERVER_PORT"] != "80") {
  $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
 } else {
  $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
 }
 return $pageURL;
}

if(stripos($currenturl,'#'))
{
    Header( "HTTP/1.1 301 Moved Permanently" ); 
    $location = 'Location: ' . str_replace('#','',$currenturl) . '/';
    Header($location);
}

* Correction - I said at the end in the Title, and I mean the end, my code here apparantly detects for the pound sign anywhere in the url. * 更正 - 我在标题的最后说,我的意思是结束,我的代码在这里可以检测到网址中任何地方的英镑符号。 * *

The part after the # is a "hash" or "fragment". #之后的部分是“哈希”或“片段”。 It is entirely client-side - it is never sent to the server (and if it is, it results in an error). 它完全是客户端 - 它永远不会发送到服务器(如果是,它会导致错误)。 As such, your PHP script will never see it, and can't therefore fix it. 因此,您的PHP脚本永远不会看到它,因此无法修复它。 This is correct behavior, and by design. 这是正确的行为,并且是设计的。

That said, you could check for the #fragment using a client-side solution (eg in JS with document.location.hash ) and redirect from there. 也就是说,你可以使用客户端解决方案检查#fragment(例如在JS中使用document.location.hash )并从那里重定向。

You could use javascript to override the click event on all the tags on your page. 您可以使用javascript覆盖页面上所有标记的点击事件。 If you're using jQuery you could do something like the following. 如果您正在使用jQuery,您可以执行以下操作。

$(function() {
  $('a').click(function(ev) {
    alert(ev.currentTarget.hash);
    return false;
  });
});

<table>
<tr><td><a href='http://google.com/#test1'>Test 1</a></td></tr>
<tr><td><a href='/game/?p=test#test2'>Test 2</a></td></tr>
<tr><td><a href='#test3'>Test 3</a></td></tr>
</table>

When the link is clicked the click event is fired and at that point you can decide what to do based on the hash from the href. 单击链接时会触发click事件,此时您可以根据href中的哈希值决定要执行的操作。 You could rebuild the href with the hash as a GET parameter and redirect the page or you could do turn it into an ajax call. 您可以使用散列作为GET参数重建href并重定向页面,或者您可以将其转换为ajax调用。

In the click event you could decide what to do with the hash tag. 在click事件中,您可以决定如何处理hash标记。 If you really need it on the server side then you could always append it to the query string or you could do an ajax call. 如果你真的需要它在服务器端,那么你总是可以将它附加到查询字符串或你可以做一个ajax调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM