Black Friday Deals Not Found Anywhere Else! Save up to 55% OFF Hosting, Domains, Pro Services, and more.
Vodien Black Friday Sale applies to new purchase on select products and plans until 4 December 2024. Cannot be used in conjunction with other discounts, offers, or promotions.
Microsoft-Top-SMB-Cloud-Master-for-March-2017-img

by

June 1, 2011

Cloud Computing, Enterprise Cloud Technology < 1 min read

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]


[template id="7325"]

Skip to section