Results tagged “redirect” from Mod Rewrite Tips and Tricks
Redirecting according to the language that a user has set is actually fairly easy. The most important thing to remember is to put the most specific first.
For example to set up site with Irish, French and English as the fallback/default:
For example to set up site with Irish, French and English as the fallback/default:
RewriteEngine onFor a full list of languages, the best I've found is from Perl's I18N-LangTags module.
# Irish UsersRewriteCond %{HTTP:Accept-Language} ^ga [NC]RewriteRule ^$ /ga/ [L,R=301]
# French UsersRewriteCond %{HTTP:Accept-Language} ^fr [NC]
RewriteRule ^$ /fr/ [L,R=301]
# Default PageRewriteRule ^$ /en/main-page [L,R=301]
It's not unusual to have multiple domains pointing at one site, possibly for brand protection or vanity domains.
If these extra domains are simply added in as aliases of the main domain, they will all appear as separate sites to search engines such as Google or Yahoo. This means that the various domains probably incur a duplicate content penalty.
Fortunately, this is easy to get around. Presuming that your primary domain name is example.com, the following can be put at the top of the .htaccess file in your document root:
As written above, example.com will appear as the canonical url rather than www.example.com. If the user does enter www.example.com, they will get redirected to example.com. To change this behavior, add in the www in the third line.
¹HTTP_HOST is the domain name that the user is using to access the site when the .htaccess file is called by Apache.
If these extra domains are simply added in as aliases of the main domain, they will all appear as separate sites to search engines such as Google or Yahoo. This means that the various domains probably incur a duplicate content penalty.
Fortunately, this is easy to get around. Presuming that your primary domain name is example.com, the following can be put at the top of the .htaccess file in your document root:
The RewriteCond line checks to see if the HTTP_HOST¹ is NOT example.com. If this is the case it will do a permanent(301) redirect to http://example.com/$1, where $1 is the part that was after the HTTP_HOST.
RewriteEngine on
RewriteCond %{HTTP_HOST} !^example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
As written above, example.com will appear as the canonical url rather than www.example.com. If the user does enter www.example.com, they will get redirected to example.com. To change this behavior, add in the www in the third line.
¹HTTP_HOST is the domain name that the user is using to access the site when the .htaccess file is called by Apache.
