简体   繁体   中英

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 "$@!" 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$@! . 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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