Removing duplicate slashes in URL via .htaccess or PHP

允我心安 提交于 2019-12-02 17:49:06

问题


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

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

do NOT work for me on a URL such as the following:

http://www.mp7.org/?site=69.com\\\\\\\\\\\\\\\\

The .htaccess file

#### mod_rewrite in use
Options +FollowSymlinks
RewriteEngine On

回答1:


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.




回答2:


This is because of IIS server, and mainly almost certainly your site is urbanized in .Net .asp or . aspx You need to put into service one miniature script, when your server be given http or https demand it must Lcase(URL). If there are not a lot of pages, you can rename all within lowercase, modify all inner linking of WebPages to lowercase and after that appeal URL removal request in Google webmaster tools.

how to remove duplicate urls



来源:https://stackoverflow.com/questions/16131109/removing-duplicate-slashes-in-url-via-htaccess-or-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!