fbpx Removing double or more slashes from URL - Meysmahdavi

Removing double or more slashes from URL

Removing double or more slashes from URL by .htaccess

If you’re getting URI requests that include the dreaded “multi forward slash”, you’re not alone. It’s an issue that has plagued many an administrator over the years. Fortunately, the situation is simple to resolve using a tasty slice of .htaccess. All you need to do is copy the following code and place it anywhere in your site’s root .htaccess file:


<IfModule mod_rewrite.c>
RewriteBase /
# 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]


Rule 1: {THE_REQUEST} contains something like GET /index.html HTTP/1.1

Hence, if we match the first whitespace (\s) followed by multiple slashes (/{2,}), we can access the correct URL without the leading double slash via $1.

Rule 2: The regular expression ^(.*)/{2,}(.*)$ splits the request URI on multiple slashes. %1/%2 then combines the two splitted strings again, but with only one slash at this time.

So for example, this directive will redirect as follows:

  • https://www.meysmahdavi.com// redirects to https://www.meysmahdavi.com/
  • https://www.meysmahdavi.com//blog-post/ redirects to https://www.meysmahdavi.com/blog-post/
  • https://www.meysmahdavi.com//path/directory/ redirects to https://www.meysmahdavi.com/path/directory/

So basically it removes the double slashes from any URL.

Logically, this code is doing the following:

  • Check if the Apache module, mod_alias, is available
  • A 301 server response code is specified and will be sent if a redirect happens
  • Regex matches two forward slashes // included at the beginning ^ of the request
  • Regex then matches any characters or no characters (.*)
  • Regex matches the end of the request string $
  • If a URL is matched, it is redirected to the specified URL
  • Any characters matched via (.*) are appended to the URL via $1 $2

That’s all there is to it. The only edit that’s required is the domain name, simply replace https://www.meysmahdavicom with your own domain and you’re good to go. Don’t forget to always test thoroughly.

We used this code you can test:


  • Share This Story

about author



Leave a Reply

Your email address will not be published. Required fields are marked *