Add Trailing Slash to the End of the URL with .htaccess Rewrite Rules

»»»Add Trailing Slash to the End of the URL with .htaccess Rewrite Rules
For a website that has URLs that end with a slash (/), it’s a good practice to ensure that all URL links been parsed by the web server ended with trailing slash, even if visitors forget to enter the ending slash. This avoid visitors been served with 404 Page Not Found or Page Cannot be Displayed error as some web servers treat links without trailing slash as a file name instead of directory, and thus unable to locate the documents. It also eliminates the possibility that both pages with same content, one with slash at the end and another without, been viewed by search engines as duplicate content.

As an example, all hits to should be redirect to

Most web server, including the popular Apache HTTPD web server supports mod_rewrite module where rules can be set in .htaccess file in order to redirect to add trailing slash to the URLs that does not already have one.

The following code can be put in .htaccess to redirect URL without trailing slash to URL with trailing slash:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !index.php
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$$1/ [L,R=301]


RewriteCond %{REQUEST_URI} !\.[^./]+$
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$$1/ [R=301,L] 


RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\..+$
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*)$1/ [R=301,L]


RewriteEngine On
RewriteBase /
RewriteRule ^([a-zA-Z0-9]+)/$ /$1 [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([a-zA-Z0-9]+)
RewriteRule ^([a-zA-Z0-9]+)$ /%1/? [R=301,L]

In you’re using CMS or blog such as WordPress that allows custom URL structure for permalinks, especially for search engine optimization (SEO), the above code should come before the block of rewrite conditions and rules for URL customization for the CMS or blog platform.

Brief explaination of the rewrite code to add trailing slash to URL

RewriteEngine On – This line enables the runtime rewriting engine based on mod_rewrite module of Apache.

RewriteBase / – This line sets the current page root directory as base URL for per-directory rewrites.

RewriteCond %{REQUEST_FILENAME} !-f – This line excludes all URLs pointing to existed files from been added with trailing slash again. Directories cannot be excluded as this would exclude the rewrite behavior for existing directories.

RewriteCond %{REQUEST_URI} !index.php – This line is optional, and will excludes a sample URL (in this case, index.php) that users do not want it to be rewritten. Remove this line if not necessary.

RewriteCond %{REQUEST_URI} !\..+$ and RewriteCond %{REQUEST_URI} !\.[^./]+$ – Specify that the URL does not contain any . (dot) to exclude reference to file.

RewriteCond %{REQUEST_URI} !(.*)/$ and RewriteCond %{REQUEST_URI} !/$ – This line determines which URL doesn’t contain a trailing slash

RewriteRule ^(.*)$$1/ [L,R=301] – This line process URL without trailing slash that has passed conditions set above, by appending a trailing slash and then redirect with 301 or permanent redirect status to the new URL. L mean this is the last line to process and the rewrite process can be terminated. Remember to replace the with your own domain name.

Brief explanation for final set of rewrite rules and rule conditions

RewriteRule ^([a-zA-Z0-9]+)/$ /$1 [L] – This line terminates the trailing slash appending rewrite process if the URL already contains trailing slash.

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([a-zA-Z0-9]+) – This line determines request that does not ends with trailing slash.

RewriteRule ^([a-zA-Z0-9]+)$ /%1/? [R=301,L] – This line append a slash to the end of URI and perform a 301 permanent redirect to the new URL with trailing slash, and terminate the rewrite block.

By | 2016-12-09T08:40:19+00:00 December 9th, 2016|Categories: Web Servers|Tags: |19 Comments

About the Author:

LK is a technology writer for Tech Journey with background of system and network administrator. He has be documenting his experiences in digital and technology world for over 15 years.Connect with LK through Tech Journey on Facebook, Twitter or Google+.
  • Pingback: Na SEO muÅ¡ke - a ukončujúce lomeno v URL | SEO chat()

  • com

    dlvrq rjdkq bfuwo jnlx

  • Killswitch

    This was extremely helpful, I have been trying for the longest time to figure out how to end a to with no luck. Had to play with it a bit due to the rules I already have, but had it working in about 5 mins.

  • Nate

    I have tried both methods and they do work but not with the other statement that I have. I was wondering exactly what Killswitch meant when he says "play with it a bit." I tried rearranging the order and that doesn't seem to help, I'm looking for a quick solution here.

  • I'd been using that slash alot on one of my website. Most of them had been indexed by search engine successfully, but four of them return a 404 not found error based on Google Webmaster report, weird.

  • Couldn't get the second option to work for me either. Getting a 500 error.

  • Thanx. Worked perfectly for me. Even wordpress uses the same code in .htaccess.

  • Arthur

    You're a legend.

    The only website I found that explained this clearly and coherently.


  • Admin

    Thank you for solution. Example is usesful for me:
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI} !..+$
    RewriteCond %{REQUEST_URI} !/$
    RewriteRule (.*)$1/ [R=301,L]

  • Patrick

    sadly, these techniques will destroy post arrays.

  • Mihai

    Tank you.

  • My fellow Filipino call center agents have urged me to do some blog surfing. What a blessing to have gone to your blog instead. I’ve found some useful information here which will definitely be of use sooner or later.

  • Pingback: Chat-Thread - Seite 1764 - XHTMLforum()

  • Hannes

    This will break urls for files that do not contain a dot and will not work for folders that do conatin a dot in their name.
    But besides of that it is the Best solution I can think of.

  • S.E

    Hi I have a question I use the code above ant it works perfect accept one case.
    For links redirects to Is it possible to add / after winter-offer not after prices/

  • Thank you for the article. I only got the last code example to work on a Sliverstripe CMS system that I am managing.

    The CMS does not redirect the non slash urls, so I am glad that I found one of the examples to work.

    Thanks again!


  • g1smd

    The URL for a “page” should NOT end with a trailing slash.

    URL ending in trailing slash denotes a folder or the index page of a folder.

    See RFC 2616, the HTTP/1.1 spec.

    Requests for “folder name without trailing slash” should be redirected to add the slash. The DirectorySlash directive already takes care of that.

  • salim

    Thanks for this solution

  • Great solution for avoiding visitors been served with the dreaded 404 Page Not Found or a Page Cannot be Displayed error.

    What about a way to avoid such unnecessary 301 redirects after adding AMP pages? is redirected to which is redirected to

    Anyone with experience in this area who could share it would be much appreciated. Likewise, I will post an update when I have the issue resolved.