Using htaccess To Force Trailing Slash

You can use htaccess to change the URL structure of your website.

There are lots of things you can do with htaccess one of the most common things would be to make sure all the pages are prefixed with the www subdomain or to make sure all the pages are not prefixed with the www subdomain. Many people often say that they want their URL structure to always have a trailing slash at the end of the URLs. This can be done by using htaccess. If the URLs don't have a trailing slash the htaccess will redirect the browser to the URL with a trailing slash.

Copy the following in your htaccess file to add a trailing slash to your website.

<IfModule mod_rewrite.c>
 RewriteCond %{REQUEST_URI} /+[^\.]+$
 RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]
Back to top

Learn how to code with Treehouse

  • Learn projects with access to 1000+ videos
  • Practice live with our Code Challenge Engine
  • Get help in our members-only forums

Start with a 7 day free trial