Posted by CANbike on Sat, 12 Jan 2013

WolfCMS: Modifying .htaccess to Automatically Add "www." to Domain Name

Wolf CMS is a nifty content management system. Out of the box it provides an option, during installation, for clean URLs (i.e. links without the “?”). A big part of this is achieved by using the apache module mod_rewrite. According to Wolf CMS installation instructions, the following .htaccess file needs to be included in the root installation directory.

.htaccess

# Setting Wolf CMS requirements
#
# On some server configurations you may need to comment out one or more of
# the three lines below due to server limitations.
#

php_flag magic_quotes_gpc off
AddDefaultCharset UTF-8
Options -Indexes +FollowSymLinks

#
# Setting rewrite rules
#

<IfModule mod_rewrite.c>
  RewriteEngine On

  # Set next line to your Wolf CMS root - if not in subdir, then just /
  RewriteBase /

  # Rules to allow install sequence to test for mod_rewrite support
  RewriteRule ^wolf/install/index.html$ wolf/install/index.php?rewrite=1 [L,QSA]
  RewriteRule ^wolf/install/index.php$ wolf/install/index.php?rewrite=1 [L,QSA]
  RewriteRule ^wolf/install/$ wolf/install/index.php?rewrite=1 [L,QSA]

  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-l
  # Main URL rewriting.

  RewriteRule ^(.*)$ index.php?WOLFPAGE=$1 [L,QSA]  
</IfModule>

.htaccess Analysed

Now I’m not familiar with .htaccess configuration, but let’s try to examine this line by line.

#
Indicates a comment.

php_flag magic_quotes_gpc off
Disables the process that automatically escapes special characters with a ‘\’ to allow a string to be entered into a database. It’s generally recommended to disable this.

AddDefaultCharset UTF-8
Sets the default character encoding to UTF-8

Options -Indexes +FollowSymLinks
Enables apache to follow symbolic links. Required by mod_rewrite. In other words, a file “/mydomain/www/filelink.html” is a symbolic link to “/mydomain/directory/realfile.html”. Apache will only retrieve the “realfile.html” if the option is enabled.

<IfModule mod_rewrite.c>
If the apache mod_rewrite module is available then execute the following code.

RewriteEngine On
Turn the rewrite engine module on.

RewriteBase /
Sets the base URL for rewrites. The “/” represents the root directory. Basically, after the rewrite rules are applied, the base URL is concatenated to the front of the result to form a new path. In a per-directory context, this is useful when a relative path is used in a substitution.

RewriteRule ^wolf/install/index.html$ wolf/install/index.php?rewrite=1 [L,QSA] RewriteRule ^wolf/install/index.php$ wolf/install/index.php?rewrite=1 [L,QSA] RewriteRule ^wolf/install/$ wolf/install/index.php?rewrite=1 [L,QSA]
These lines are used only for the installation of Wolf CMS. Afterwards, they have no effect.

RewriteCond %{REQUEST_FILENAME} !-f
Rewrite condition. If this is not a file.

RewriteCond %{REQUEST_FILENAME} !-d
Rewrite condition. If this is not a directory.

RewriteCond %{REQUEST_FILENAME} !-l
Rewrite condition. If this is not a symbolic link.

RewriteRule ^(.*)$ index.php?WOLFPAGE=$1 [L,QSA]
Here’s where the rewrite magic happens. A further explanation is required.

RewriteRule Examined

RewriteRule ^(.*)$ index.php?WOLFPAGE=$1 [L,QSA]

^ - pattern matches from beginning of string
( - start of subpattern
. - one or more of ...
* - ... any character
) - end of subpattern
$ - end of string
^(.*)$ - The requested URL without the domain
L - the last rule in this run
QSA - Query String Append

The subpattern "^(.*)$" will then be passed onto index.php?WOLFPAGE as $1 parameter. From [L,QSA], the L indicates the last rule, in this run; and, the QSA indicates that any additional query string patterns should be appended at the end.

For example, the clean URL domain.com/directory/article.html would be rewritten to domain.com/?index.php?WOLFPAGE=directory/article.html for apache to fetch the actual webpage from. The only issue is if that page does exist.

Redirecting from Root Domain to www Subdomain

Typing in the either URL domain.com or www.domain.com points to the same page. Most internet users will immediately recognize the website as being the same based on the domain name. However, there are still two different addresses pointing to the same page.

Wolf CMS already allows for clean URLS. For aesthetic reasons, and consistency, it would be even nicer to also redirect the root domain to the www subdomain. In other words, automatically add the www if it is not there.

This can be achieved by adding the following lines of code to the .htaccess file.

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

Before doing this, let’s examine the code.

RewriteCond %{HTTP_HOST} !^www\. [NC]

^ - pattern matches from beginning of string
%{HTTP_HOST} - host name (i.e domain.com)
! - not
^ - pattern matches from beginning of string
[NC] - Conditional pattern is case insensitive.
\. - "." The "\" is an escape character. "." by itself has a special meaning.

The first line sets the conditional rule. If the host name, %{HTTP_HOST}, does not start with “www.” (case insensitive), then set to true for the rewrite rule.

RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]

^ - pattern matches from beginning of string
( - start of subpattern
. - one or more of ...
* - ... any character
) - end of subpattern
$ - end of string
^(.*)$ - The requested URL without the domain
$1 - the token that was matched in the parenthesis
R=301 - HTTP 301 code. Permanent redirection.
L - the last rule in this run

If the RewriteCond is true, permanently redirect the requested URL (without the domain) $1 to http://www.domain.com/$1.

Of course for this to work, the module mod_rewrite needs to be loaded and turned on. Moreover, the most logical place to put this code is before the clean URL rewrite. In other words, if the subdomain www is lacking add it first, then point the resulting clean url to the actual page. The whole point is to deal with one address, and not two different addresses.

The final .htaccess file

#
# Setting Wolf CMS requirements
#
# On some server configurations you may need to comment out one or more of
# the three lines below due to server limitations.
#

php_flag magic_quotes_gpc off
AddDefaultCharset UTF-8
Options -Indexes +FollowSymLinks

#
# Setting rewrite rules
#

<IfModule mod_rewrite.c>
  RewriteEngine On

  # Set next line to your Wolf CMS root - if not in subdir, then just /
  RewriteBase /

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

  # Rules to allow install sequence to test for mod_rewrite support
  RewriteRule ^wolf/install/index.html$ wolf/install/index.php?rewrite=1 [L,QSA]
  RewriteRule ^wolf/install/index.php$ wolf/install/index.php?rewrite=1 [L,QSA]
  RewriteRule ^wolf/install/$ wolf/install/index.php?rewrite=1 [L,QSA]

  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-l
  # Main URL rewriting.

  RewriteRule ^(.*)$ index.php?WOLFPAGE=$1 [L,QSA]  
</IfModule>

Just One More Step

Wolf CMS just needs a minor tweak in it’s configuration file config.php.

Find the line:
define('URL_PUBLIC', 'http://domain.com');

If the www subdomain is not there, then change it:
define('URL_PUBLIC', 'http://www.domain.com');

Be sure to use your actual domain name and not “domain.com”.

Lastly, for full documentation on writing .htaccess files visit http://httpd.apache.org/docs/current/


Related Item(s):