fbpx Removing that pesky trailing slash | ComputerMinds Skip to main content

Removing that pesky trailing slash

11th Nov 2010

Steven Jones

Senior Developer

So, it's perfectly valid to visit a site using a URL like:

http://www.google.co.uk///////////

But some pesky SEO types will complain that the site is accessible at two URLs and that you need do a 301 redirect to the canonical URL (http://www.google.co.uk/). What you want to do is remove the trailing slashes using mod rewrite.

If you really need to do this, then you can just pop the following in the .htaccess file that Drupal provides:



  # Remove multiple slashes after domain
  RewriteCond %{HTTP_HOST} !=""
  RewriteCond %{THE_REQUEST} ^[A-Z]+\s//+(.*)\sHTTP/[0-9.]+$
  RewriteRule .* http://%{HTTP_HOST}/%1 [R=301,L]

just before the # Rewrite URLs of the form 'x' to the form 'index.php?q=x'. line.

This should redirect any visitors with extra slashes after the domain to the canonical URL without slashes.

Now you can get back to real development, go!