2

I am passing an URL with validating the slash in end index. But the problem is the slash may be present or not. so I need to pas only url wheather the slash in last index prsent or not.

The URL may be like

https://www.instagram.com/example/    (with slash endIndex) or
https://www.instagram.com/example     (without slash endIndex)

I need the URL as only

https://www.instagram.com/example    (without slash)

Thanks for your help

Sibasankar Bhoi
  • 589
  • 4
  • 14
  • You can adjust your [Apache Config](https://stackoverflow.com/questions/21417263/htaccess-add-remove-trailing-slash-from-url) or [Nginx Config](https://serverfault.com/questions/597302/removing-the-trailing-slash-from-a-url-with-nginx) to handle this for you. If you are using something locally like Laravel Valet with certain top-level domains you might need to make additional adjustments. – Chris G Jan 25 '18 at 13:51

3 Answers3

4

You can use rtrim():

$url = rtrim($url, '/');

Strip whitespace (or other characters) from the end of a string

http://php.net/manual/en/function.rtrim.php

Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
0

You can use any of the following,

$string = "Remove Last Characterr";
substr($string, 0, -1);
substr_replace($string ,"",-1);
rtrim($string,'r');

Output: Remove Last Character

Ahmar Arshad
  • 477
  • 1
  • 10
  • 22
0

To avoid duplicating canonical URLs which can downsize your SEO points, consider adding this rule to your .htaccess file:

# Removes trailing slashes (prevents SEO duplicate content issues)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ $1 [L,R=301]

This way you'll never have trailing slash in URL Just different approach on solving your issues in question.

Tpojka
  • 6,996
  • 2
  • 29
  • 39