Skip to main content

Removing that pesky trailing slash

An article from ComputerMinds - Building with Drupal in the UK since 2005
11th Nov 2010

Steven Jones

Senior Developer
Steven Jones
Hey, you seem to look at this article a lot! Why not Bookmark this article so you can find it easily in the future?

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!

Hi, thanks for reading

ComputerMinds are the UK’s Drupal specialists with offices in Bristol and Coventry. We offer a range of Drupal services including Consultancy, Development, Training and Support. Whatever your Drupal problem, we can help.