Tarun Keswani
Tarun Keswani

Reputation: 39

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.

Upvotes: 0

Views: 648

Answers (2)

Jon Lin
Jon Lin

Reputation: 143856

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.

Upvotes: 3

karllindmark
karllindmark

Reputation: 6071

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

Upvotes: 1

Related Questions