I wanted to have the following construction: one CMS for maintenance (domain + sub domain are pointing to the same install directory of CMSMS), two sites with identical page hierarchy but different templates showing different content (lesser blocks and tags optimized mobile and pda clients).
I want pretty URLs all the way so I searched for a way on how to select a template and how to avoid any extra parameters for the user. Additionally I didn't want to do it with cookies or limit the alterations to CSS.
I read that there is a simple way to capture page variables with smarty and show stuff only if a specific variable contains a specific value.
E.g.
Code: Select all
{if $smarty.get.template == 'mobile'}
Stuff to do if extra parameter "template" value matches 'mobile'. The full mobile/pda version template.
{else}
Contains the normal web browser template if no extra parameter is given.
{/if}
E.g.
Code: Select all
{content assign="content_var"}
{content block="mobile_content" assign="mobile_content_var"}
{content block="reg_content" assign="reg_content_var"}
Code: Select all
{content assign="content_var"}
{content block="mobile_content" assign="mobile_content_var"}
{content block="reg_content" assign="reg_content_var"}
{if $smarty.get.template == 'mobile'}
<!DOCTYPE ...
<__html ... >
...
{$content_var}
{$mobile_content_var}
{custom_tag_1 output="mobile"}
...
</__html>
{else}
<!DOCTYPE ...
<__html .. >
...
{$content_var}
{$reg_content_var}
{custom_tag_1 output="regular"}
{custom_tag_2}
...
</__html>
{/if}
So next: modify my .htaccess to extend the URL with an extra parameter - if it is called from a sub domain called "mobile"
-> e.g. http://mobile.domain.tld/home.htm will be rewritten to http://www.domain.tld/index.php?page=ho ... ate=mobile.
Code: Select all
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
# REWRITE TO "home.htm" IF REQUEST DOESN'T CONTAIN PAGE
# ==================================================
# Checks if requested URI is "empty" and rewrites
# it from http://www.domain.tld to
# http://www.domain.tdl/home.htm
# ==================================================
RewriteCond %{REQUEST_URI} ^/$ [NC]
RewriteRule .* /home.htm
# ADD EXTRA PARAMETER IF PAGE IS LOCATED UNDER SPECIFIC SUB DOMAIN
# ==================================================
# Checks if domain is "mobile.domain.tld"
# Rewrites urls in the form of /parent/child/
# but only rewrites if the requested URL is not a file or directory
# additionally appends the parameter "template" with the value="mobile"
# to the query string. Stops rewriting afterwards
# ==================================================
RewriteCond %{HTTP_HOST} ^mobile\.domain\.tld$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteRule ^(.+).htm$ index.php?page=$1&template=mobile [QSA,L]
# FOR REGULAR SITE
# ==================================================
# Rewrites urls in the form of /parent/child/
# but only rewrites if the requested URL is not a file or directory
# ==================================================
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteRule ^(.+).htm$ index.php?page=$1 [QSA]
The second rule checks if the http host is mobile.domain.tld prior to execution of rule, and if that is true, rewrites pretty URLs to the CMS query and appends the extra variable I need for identifying the part of the template I want to show.
The third rule applies if the page is not located under mobile.domain.tld, so always when we look at domain.tld. The third rule is taken from the CMSMS documentation
So http://mobile.domain.tld/article.htm gives me the mobile version of http://www.domain.tld/article.htm.
I had to set the navigation menu templates to display relative page links instead of absolute ones, so I can navigate freely within a sub domain by using:
Code: Select all
...href="{$node->alias}.htm"...
Code: Select all
...href="{$node->url}"...
Since I am not that much of a programmer or administrator I guess that this solution could still be tweaked and optimized. Now I need to see if I encounter any major problems. But from first sight it looks good.
Now I can add an article and I instantly get a mobile version of it in my sub domain. All automatic. For the editor it looks like one site that is maintained without extra branches or extra work for mobile content. Well, doesn't necessarily have to be a mobile template... you can use this for whatever reason you want to use different templates for.
What do you think about the approach? What would you do different? Do you see any downsides I might have overlooked?
best
nhaack