14
Nov
2014

Force WWW and Trailing Slash With .htaccess File

posted Friday, November 14th 2014 at 8:23 AM by

htaccess file

I was doing some work in Google Webmaster Tools and saw an opportunity to make some enhancements to Homebräu CMS. It seemed that pages were inconsistent, some being listed as http://www.admakonieska.com and others listed as just http://adamkonieska.com. The second problem was sometimes if a user left off a trailing slash, the page wouldn't display. E.g. http://www.adamkonieska.com/archives/ would work, but http://www.adamkonieska.com/archives would throw a 404 error.

Nothing a few changes to the htaccess file wont fix. Here are two code snippets for forcing both the www and the trailing slash on URLs.

RewriteEngine on


################
#              #
#	Force WWW  #
#              #
################

RewriteCond %{HTTP_HOST} !^(www) [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]


##########################
#                        #
#  Force trailing slash  #
#                        #
##########################

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^(.*)\.[(.*)]{0,4}(.*)$ [NC]
RewriteRule ^(.*)([^/])$        /$1$2/ [L,R=301]
Notes about Forcing WWW

RewriteCond %{HTTP_HOST} !^(www) [NC] - The first line sets a rewrite condition that looks for any page not starting with a www, and as indicated by the [NC], this match is regardless of case (www or WWW).

RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301] - The rewrite rule tells the server to take whatever the URL was, and append it to http://www.example.com/. If you're new to rewrite rules, corz.org does a really good job of breaking down the parts of an htaccess file so you can understand what each bit is doing.

RewriteCond %{REQUEST_URI} !^(.*)\.[(.*)]{0,4}(.*)$ [NC] - This line is only required if you are spoofing filenames with your htaccess. For example, if you have a file called foo.php, but you're serving it as foo.html with your htaccess file, you need this additional line to prevent the trailing slash from being added.

Notes About Forcing the Trailing Slash

RewriteCond %{REQUEST_FILENAME} !-f - the only thing this rewrite condition is looking for, is to make sure the URL being requested is not a file. So anything that actually exists on the server will not be affected, including images, .php, .html, etc.

RewriteRule ^(.*)([^/])$ /$1$2/ [L,R=301] - the rewrite rule says to match any requested URL that doesn't end in a slash, and append the trailing slash. It also indicates that its the last rewrite rule for the condition above, and that it should return a 301 permanently moved message for the originally requested URL, via [L,R=301]

Final Thoughts

Pretty simple fix to two fairly common htaccess problems. One final note, make sure to change out www.example.com for your domain, and remember to include the RewriteEngine on.

Share This:

Tags:

Comments:

View (0) Comments Post a Comment
  • Replying to Adam Konieska on Force WWW and Trailing Slash With .htaccess File