Hello! There is a site where in the address bar you can enter instead of one / two or more, and the pages will still open (example: test//home///page ). It is necessary to do so that if the address bar contains two or more / in a row, refer to the same page only with normal slashes (for example: test/home/page ). I tried to do this:

  $pos = strpos($_SERVER['REQUEST_URI'], '//'); if($pos != '') { $link = str_replace ( '//', '/', $_SERVER['REQUEST_URI']); header('Location: ' . $link .''); } 

For some reason, it does not take into account the slashes at the very beginning of the address (for example: test//home/page ).

  • RTFM php.net/strpos - Ipatiev
  • @ Ipatiev, for some reason, strpos does not see the first entry - r.mcreal
  • RTFM php.net/strpos - Ipatyev
  • @ r.mcreal, give an error 404 to such addresses, and not redirection. - Visman
  • @Visman, redirection is needed - r.mcreal

2 answers 2

Here is the solution for you .htaccess for apache:

 RewriteEngine On RewriteCond %{REQUEST_URI} // RewriteRule ^(.*)$ http://your.site/$1 [R,L,QSA] 

Based on this answer.

  • is it just in .htaccess you need to add? I haven’t worked with him before - r.mcreal
  • @ r.mcreal, if you have an apache server, then yes, in .htaccess, which is at the root of the site. If there is already RewriteEngine On , then immediately after it you register 2 and 3 lines from this answer. - Visman

use substitute regular

 $link = preg_replace('/\/+/', '/', $_SERVER['REQUEST_URI']); 

But it looks crooked (toothpick), to the poetmo, usually written like this:

 $link = preg_replace('!/+!', '/', $_SERVER['REQUEST_URI']); 

But it is better to solve similar problems using apache / nginx.

  • Writes: Сайт test выполнил переадресацию слишком много раз. - r.mcreal
  • and how could he do that. He has to do it only once - r.mcreal
  • redirection occurs many times because the if condition is a bit strange. If strpos finds nothing, it returns false. - KoVadim
  • Thank you very much! Before that I did this: `if ($ pos! = False) {...} , но забыл что в этом случае false` would be equal to 0 ie to the very first element, now I am comparing so `if ($ pos! == false) {...}` and everything works - r.mcreal
  • @ r.mcreal did not "forget", and they stumbled your nose - Ipatyev