Handle Trailing slashes at the end of URL

There is clear cut advantage of using consistent URL structure for wordpress website.

I want all of my URL’s to end with / (the homepage and internal page URL’s). Let me give an example website which is handling it very efficiently so that the URL’s without ending with slash or ending with multiple slashes are 301 redirected to URL with single slash.

Read More

http://viralpatel.net/blogs 301 redirect to viralpatel.net/blogs/

http://viralpatel.net/blogs// 301 redirect to viralpatel.net/blogs/

http://viralpatel.net/blogs/ 200 OK

http://viralpatel.net/blogs/check-string-is-valid-date-java 301 redirect to http://viralpatel.net/blogs/check-string-is-valid-date-java/

http://viralpatel.net/blogs/check-string-is-valid-date-java// 301 redirect to http://viralpatel.net/blogs/check-string-is-valid-date-java/

http://viralpatel.net/blogs/check-string-is-valid-date-java/ 200 OK

Any idea what .htaccess rules can help achieve this. My current .htaccess looks like:

    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^javaexperience.com [NC]
    RewriteRule ^(.*)$ http://www.javaexperience.com/$1 [R=301,L]
    RewriteBase /
    RewriteRule ^index.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d    
    RewriteRule . /index.php [L]

Related posts

Leave a Reply

1 comment

  1. You’re not going to be able to handle the URLs that end with // because the slashes are normalized before the URI is processed, so mod_rewrite will never be able to match the // (it’ll only see a single /). To handle this, you may need something on the browser’s side. For the no slash, add this right above the RewriteBase line in your current htaccess:

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