Reputation: 155
I've got a situation that I won't be able to apply htaccess on, particularly this URL:
www.site.com/#hash
which triggers an important js function.
Some referrers are sending traffic to www.site.com/#hash/ <-- notice the trailing slash
I need a quick-fix solution (PHP would be my first choice, and JS second) that will strip away any final '/' from the URL while leaving any kind of hash (e.g., /#hash1, #hash2, #otherhash, etc.) intact.
I've tried a few dozen things and so far I still haven't found a good way to target/modify/remove only the '/' and nothing else.
For example, I'm pretty sure this could work:
$url = preg_replace('{/$}', '', $url);
...but I don't know how to aim it at the actual url in the browser's address bar.
And, with JS, for example, pseudocode:
if (window.location.hash.CONTAINS == '/') {window.location.hash = ''}
In this case I can't figure out how to target only the possible '/' at the end.
Upvotes: 1
Views: 339
Reputation: 173582
This is a problem that can't be solved in PHP. You must use JavaScript.
Use String.prototype.indexOf()
:
if (location.hash.indexOf('/') != -1) {
// remove the slash
location.hash = location.hash.replace('/', '');
}
If it needs to be specifically the last slash:
if (location.hash.substr(-1) == '/') {
location.hash = location.hash.slice(0, -1);
}
Upvotes: 1
Reputation: 149030
Try use String.prototype.replace
to do a regex substitution:
window.location.hash = window.location.hash.replace(/\/$/, '');
Upvotes: 0