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 http://www.mydigitallife.info/contact should be redirect to http://www.mydigitallife.info/contact/.

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 ^(.*)$ http://domain.com/$1/ [L,R=301]

or

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

or

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} !\..+$ - Specify that the URL does not contain any . (dot) to exclude reference to file.

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

RewriteRule ^(.*)$ http://www.domain.com/$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 www.domain.com with your own domain name.

Brief explaination for second 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.

Share and contribute or get technical support and help at My Digital Life Forums.



One Response to “Add Trailing Slash to the End of the URL with .htaccess Rewrite Rules”

  1. Na SEO muške - Vybrali.sme.sk a ukončujúce lomeno v URL | SEO chat
    July 26th, 2008 05:19
    1

    [...] čím predídete aj problému č.2. Pre htaccess som našiel pekný, ale anglický článok o tom, how to add trailing slash to the end of the URL. Možnosti máte [...]

Leave a Reply

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Subscribe without commenting


Custom Search

New Articles

Incoming Search Terms for the Article

rewriterule add slash - htaccess directory slash - trailing slash - htaccess trailing slash - rewriterule slash - trailing slash 301 - url slash - .htaccess add trailing slash - apache append slash - apache rewrite ending slash - seo trailing slash - mod_rewrite slash at the end of the url - 301 trailing slash - .htaccess slash - rewriterule trailing slash - wordpress trailing slash apache - htaccess with without slash - trailing slash end of url - .htaccess redirect add slash to url - add trailing slash and redirect - add / at the end of the url htacces - htaccess add www to url - mod rewrite slash problem base url - rewrite rule end slash - rewrite rule add end slash - url without trailing slash gives 301 - url with slash at the end - url add - add slash to url - apache rewrite request uri ends with slash - mod rewrite add slash to end of uri - rewriterule htaccess remove trailing slash - rewrite rule portable url - rewrite url add trailing slash - rewriterule trailing / - seo urls with or without slash - URL without trailing "/" - URL trailing / - url error outlook 2003 four slashes - what is a trailing slash - what is the purpose of end slash in rewriting - wordpress not adding trailing slash - 301 htaccess directory sync - add slash to url php - apache rewritecond require www trailing // - auto adding slash .htaccess - htaccess append trailing slash - htaccess rewrite condition - joomla append slash before url - mod_rewrite rewritebase trailing slash -