开发者

Removing part of a string in a query string in a URL

开发者 https://www.devze.com 2023-03-24 20:49 出处:网络
I have an input URL that looks something like this: http://localhost/20north/Numark/product/1/B$@!00$@!4JPPO94$@!

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 "$@!" 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 开发者_StackOverflow社区not just B$@!00$@!4JPPO94$@!. Also, the position of $@! can be anywhere in that last part.


Using mod_rewrite, you just need this rule:

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

Edit:

Actually, there seems to be a problem when the $@! is at the end of the 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 
    $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

0

精彩评论

暂无评论...
验证码 换一张
取 消