Powered by Cellfex RSS Follow me on Spotify

Archive for the ‘htaccess’ Category

There are some issues when using www domains with non-www domains (like javascript initialization which results in cross-domain incompatability when www.domain.com is understood as something else than domain.com).

Anyway, www is soooo ’90.

To get rid of www and transfer customers to non-www without rewriting your html/php files just put a .htaccess rewrite rule:

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.domain.com [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301]
19 May 2011

Htaccess www-domain to non-www domain redirect

Author: Edgars | Filed under: Web Development, htaccess

Instead of writing custom snippets for php headers its faster to redirect website visitor to “his” language by using .htaccess rewriting rules (mod_rewrite should be enabled though and certain permissions allowed for you to change htaccess directives)

so, lets imagine you already have language support on website by entering corresponding language as first argument in url:

http://yourdomain/en/index/ for english speaking visitors
http://yourdomain/fr/index/ for french speaking visitors

in order to redirect these visitors by default when they enter a site, you may write tricky cookies or PHP snippets that grab User Agent Language, but for me somehow this approach turned out to be cool enough in order not to make site engine not to support feature that is needed in one of 30 pages.

RewriteEngine on of course is already there since i already have user friendly url’s, so i add these lines to .htaccess

RewriteCond %{HTTP:Accept-Language} ^fr [NC]
RewriteRule ^$ fr/index/ [L,R=301]
RewriteRule ^$ en/index/ [L,R=301]

First line detects “Accept-Language” header in user agent and if it is “fr*” for any french speaking visitor, i redirect him to french site on second line.
Third line is a fallback for default English language site.

IF you dont have friendly url’s but want to start having some right now, then just add RewriteEngine on on .htaccess file in your site root.

RewriteEngine on
RewriteCond %{HTTP:Accept-Language} ^fr [NC]
RewriteRule ^$ index.php/?language=fr [L,R=301]
RewriteRule ^$ index.php/?language=en [L,R=301]

Htaccess is a tricky thing, so you might need additional Options but that goes beyond this post. Cheers!

23 Feb 2011

Htaccess redirect by user language

Author: Edgars | Filed under: DailyStuff, Web Development, htaccess