Double slashes are usually caused by redirect rules. For example RewriteRule ^(.*)$ http://www.example.com/path/$1 [R=301,L]
could cause the double slash. The regular expression's parenthesis might be returning /filename.html
for the $1
variable. The two slashes come from one from the match and the one hard coded.
To make this redirect work as expected, you could have the rule start with an optional slash that isn't captured: RewriteRule ^/?(.*)$ http://www.example.com/path/$1 [R=301,L]
Then the captured group wouldn't contain the leading slash. Alternately, you could remove the second hard coded slash and rely on the captured group having the slash: RewriteRule ^(.*)$ http://www.example.com/path$1 [R=301,L]
.
I prefer the first of those two solutions. Rewrite rules match leading slashes differently depending on which context they are used in. The URL path does not have a leading slash when rewrite rules are used in .htaccess
but the path does have a leading slash when the rewrite rules are used in httpd.conf
. Making the leading slash optional with ^/?
allows the rule to work correctly in either context.
The problem is compounded by relative links once a crawler finds a URL with a duplicate slash. If http://www.example.com/path//filename.html
contains a link like <a href="otherfile.html">
the the crawler will also fetch http://www.example.com/path//otherfile.html
. Crawlers will find fewer duplicate slashes on your site if you use "root relative links" on your site. Root relative links start with a slash and link relative to your domain name. If the link were <a href="/path/otherfile.html">
then the crawler wouldn't find additional duplicate slash URLs from one malformed URL.
Once a crawler has found URLs with duplicate slashes, it may continue to crawl them indefinitely, even after you fix all the redirect rules and links. It is a good idea to add rules to remove double slashes from URLs. It looks like this rule set from Issue In Removing Double Or More Slashes From URL By .htaccess is a good one:
# rule 1: remove multiple leading slashes (directly after the TLD)
RewriteCond %{THE_REQUEST} \s/{2,}
RewriteRule (.*) $1 [R=301,L]
# rule 2: remove multiple slashes in the requested path
RewriteCond %{REQUEST_URI} ^(.*)/{2,}(.*)$
RewriteRule (.*) %1/%2 [R=301,L]
xml
for errors.