Removing duplicate slashes in URL via .htaccess or PHP

前端 未结 2 765
刺人心
刺人心 2021-01-26 10:00

I\'m trying to remove duplicate slashes from URLs. The following .htaccess rule:

RewriteRule ^(.+)//+(.*)$ $1/$2 [L,NC,R=301] 

do NOT work for

2条回答
  •  独厮守ぢ
    2021-01-26 10:25

    This rule won't work for backslashes. You must add a similar rule with backslashes

    RewriteRule ^(.+)\\\\+(.*)$ $1\\$2 [L,R]
    

    If you want to replace backslashes with (forward) slashes, use this rule instead

    RewriteRule ^(.+)\\\\+(.*)$ $1/$2 [L,R]
    

    And to remove all back-/slashes at the end of the request

    RewriteRule ^(.*?)[/\\]+$ $1 [L,R]
    

    and the same when it is a query string

    RewriteCond %{QUERY_STRING} ^(.*=.*?)[/\\]+$
    RewriteRule ^.*$ $0?%1 [R,L,QSA]
    

    When everything works as you expect, you can change R to R=301.

    Never test with 301 enabled, see this answer Tips for debugging .htaccess rewrite rules for details.

提交回复
热议问题