How To Solve The Trailing Slash Issue In Htaccess

How To Solve The Trailing Slash Issue In Htaccess

A particular site had VBulletin installed within a sub-folder (/forums/) and it had a problem with parsing URLs. The following chunk of code was the contents of the .htaccess file. What it did was to redirect all non-www URLs to their respective URLs with www. appended to the front of the URL.

Options +FollowSymLinks
RewriteEngine On

# This redirects non-www to www
RewriteCond %{SERVER_NAME} ^yourdomain.com [NC] RewriteRule ^(.*)$ http://www.yourdomain.com/forums/$1 [L,R=301]

Specifically, forum posts and categories and what-not were fine, e.g.:

http://www.yourdomain.com/forums/category-one
http://www.yourdomain.com/forums/sample-post.php

However, if you tried to access http://www.yourdomain.com/forums/, Apache would act up and give you a weird-looking URL, resulting in an error 404 page.

Turns out that the addition of the following line is a conditional statement that tells Apache to skip the rule for anything that ends with a slash.

RewriteCond $1 !^/

The resultant .htaccess code:

Options +FollowSymLinks
RewriteEngine On

# This redirects non-www to www
RewriteCond %{SERVER_NAME} ^yourdomain.com [NC] RewriteCond $1 !^/
RewriteRule ^(.*)$ http://www.yourdomain.com/forums/$1 [L,R=301]


BILL POH

Senior Tech Writer for Vodien Internet Solutions

Bill Poh is the Senior Tech Writer at Vodien and he covers web hosting, online marketing, social media, business and the latest tech innovations. When not writing, he’s busy pursuing his passion for photography, graphic design, and creative arts.

Here are some of the other pages created by Bill Poh:

Facebooktwittergoogle_pluslinkedinmail

Leave a Reply

Articles in Picture

Singapore’s Best

Vodien offers the best Singapore web hosting experience around with the best shared web hosting plans, dedicated servers, and VPS plans.
Vodien SuperSupport