Wordpress, Themes, htaccess and me

by Riccardo Capecchi

First, thanks to Guillermo for 2 things, his nice review about Linuxaria And second for the opportunity he gave to me to make a guest post in go2linux, a site that i personally consider really interesting and that i know has many fans.

In this article we’ll see how to use the .htaccess to check the user-agent of the browser that is connecting to our site and set a cookie based on that value. First, what’s the .htaccess file ?

.htaccess file are part of the configuration of apache web server, most of the Linux hosting provider use and .htaccess it’s a way to give to the users a way to change the configuration for their website.

From Apache documentation:

.htaccess files (or "distributed configuration files") provide a way to make configuration changes on a per-directory basis. A file, containing one or more configuration directives, is placed in a particular document directory, and the directives apply to that directory, and all subdirectories thereof.

So, the .htaccess file has to be placed in your document root, where your CMS start, this could be the main public_HTML or a subdirectory.

How i used it on my site

If you have a CMS like Wordpress, Joomla or Drupal probably you are already familiar with the file .htaccess, because these CMS use it for many things, for example to rewrite the urls in a more friendly SEO format.

All started when i changed the Wordpress theme of my site, moving to Suffusion a modern and nice theme but that is not so correctly parsed from some Browser like Konqueror and Midori, the result with these 2 browsers it's a grey page, the page is downlaoded but don't show up correctly. I've no problems with Firefox, Chrome and Opera (no one visiting my site complains about IE), and the result of the site is nice for me.

So i could :

  1. Go back to the Old Site, wasting some hours of configuration of the new theme
  2. Ignore these browsers, and just care for the more used.
  3. Something else

I really like the new theme, so option 1 was discarded.

The option number 2 is not a good option for me, i've always liekd Linux because there are so many ways to do teh same thing.. and now i limit the sue of some browsers ? Not an option.

And so option 3, try to do somethign else, i knew that the old Theme worked fine with all of these browsers, so what about a dynamic theme choice dipending from the browser ? My site had to be able to recognize the browser and dynamically choose between the 2 theme the new and the old.

What's ready in Wordpress

As first option i checked if wordpress had some plugin ready to use to switch theme. I found some interesting plugins, a lot are related to mobile and switching theme for these devices, and at the end i installed this theme-switcher

It's a nice plugin that permit to the users to switch theme with a dropdown menu it work fine, but this don't resolve my problem, an user with konqueror (for example) simply cannot see the page and so he cannot choose the theme.

But while playing with this plugins i noticed a thing, the plugin work setting up a cookie: wptheme02778a360be726cc7b17631d120c98f5 that contains the name of the theme.

So i need a way to set up that cookie, before the user arrive on my site.

My solution

Apache with the mod_rewrite can do a lot of wonderful and complicated things, and it can check for the User-Agent that is connecting, and check and/or set cookie in the user's browser.

So in short, this is my solution:

0 RewriteEngine On
1 RewriteCond %{HTTP_COOKIE} !^.*wptheme.*$ [NC]
2 RewriteCond %{HTTP_user_agent} ^.*Konqueror.*$ [OR,NC] 
3 RewriteCond %{HTTP_user_agent} ^.*Midori.*$ [OR,NC] 
4 RewriteCond %{HTTP_user_agent} ^.*Safari.*$ [OR,NC] 
5 RewriteCond %{HTTP_user_agent} ^.*AppleWebKit.*$ [NC] 
6 RewriteRule ^(.*)$ /$1 [co=wptheme02778a360be726cc7b17631d120c98f5:brownline:www.linuxaria.com:7200:/]

These apache directive means:

0) As first thing turn on the rewrite engine for this session.
1) Check if the cookie wptheme is already set
2,3,4,5) Check if the connecting user-agent is one of these listed, i could have done that in 1 line with a regular expression,
but as tutorial having many lines is more readable.
6) This is the rule that apply if conditions (1 and (2 or 3 or 4 or 5) ) are met; basically it don't touch the URI but set a cookie into the browser with:

Name: wptheme02778a360be726cc7b17631d120c98f5 Value: brownline Domain: www.linuxaria.com Expiration: 72000 minutes That's exactly the same format used by the WordPress plugins that recognise it and show the correct theme.

In Conclusion

If your hosting solution it's based on Apache you can take advantage of .htaccess and the rewrite directives.
With them you can check and set cookie, or send people to a specific url if they have a cookie set (for example you could use this solution to have a site in multiple languages), or you can check the user-agent of teh browsers like into my solution.

I suggest this short article, to take a look at the potential of these directives, what's really important to understand is that with these solutions you can have potent solutions without overloading your CMS with plugins or worst custom code that you must write and maintain over time.

For a complete list of the name of all user-agent check this site