简体   繁体   English

删除URL中查询字符串中的一部分字符串

[英]Removing part of a string in a query string in a URL

I have an input URL that looks something like this: 我有一个输入网址,看起来像这样:

http://localhost/20north/Numark/product/1/B$@!00$@!4JPPO94$@!

While redirecting this to a new URL, I need to find and remove all occurrences of "$@!" 将其重定向到新的URL时,我需要查找并删除所有出现的“ $ @!”。 from the last part of the url, so that it becomes: 从网址的最后一部分开始,这样它就变成了:

http://localhost/20north/Numark/product/1/B004JPPO94

Note: The last part can be anything and not just B$@!00$@!4JPPO94$@! 注意:最后一部分可以是任何内容,而不仅仅是B$@!00$@!4JPPO94$@! . Also, the position of $@! 另外, $@!的位置$@! can be anywhere in that last part. 可以在最后一部分中的任何地方。

Using mod_rewrite, you just need this rule: 使用mod_rewrite,您只需要以下规则:

RewriteRule ^(.*)\$@!(.*)$ $1$2 [N]

Edit: 编辑:

Actually, there seems to be a problem when the $@! 实际上,当$@!时似乎存在问题$@! is at the end of the URI. 在URI的末尾。 Adding an extra rule to remove the trailing match seems to fix it: 添加额外的规则以删除结尾的匹配似乎可以解决此问题:

RewriteRule ^(.*)\$@!$ $1
RewriteRule ^(.*)\$@!(.*)$ $1$2 [N]

Not quite sure why that was happening. 不太清楚为什么会这样。

If you're using php, you could do the following: 如果您使用的是php,则可以执行以下操作:

<?php 
    $this_url = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
    if( strpos($this_url, '$@!') !== false ) 
       die(header('Location: ' . str_replace('$@!', '', $this_url))); 
?>

Edit: updated the code to become dynamic 编辑:更新代码以使其变为动态

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

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