Mobile app version of vmapp.org
Login or Join
Nimeshi995

: Htaccess rewrite url for language I need help for rewrite url via htaccess by adding language. I found this example and work fine: .htaccess language redirects with SEO-friendly URLs However I

@Nimeshi995

Posted in: #Htaccess #Seo

I need help for rewrite url via htaccess by adding language.
I found this example and work fine:
.htaccess language redirects with SEO-friendly URLs

However I need something more generic, for avoid to add all language code on htaccess.

Something like:

RewriteCond %{HTTP:Accept-Language} ^([a-zA-Z]{2}) [NC]
RewriteRule ^$ // [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l

RewriteRule ^.*$ index.php [L]


I would like also know that maybe I can do this via PHP? Or for SEO is better use htaccess? Or it's the same? But maybe this is another question in relation with SEO...

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Nimeshi995

1 Comments

Sorted by latest first Latest Oldest Best

 

@Alves908

RewriteCond %{HTTP:Accept-Language} ^([a-zA-Z]{2}) [NC]
RewriteRule ^$ // [L,R=301]



Almost, you just need to use %1 instead of , as a backreference to the last matched CondPattern (RewriteCond directive pattern), as opposed to the RewriteRule pattern (which is what refers to).

You also don't need the NC (nocase - case insensitive match) flag if you are specifying lower and uppercase in the pattern, or remove uppercase from the pattern. In other words:

RewriteCond %{HTTP:Accept-Language} ^([a-z]{2}) [NC]
RewriteRule ^$ /%1/ [L,R=301]


Clear your browser cache before testing.


I would like also know that maybe I can do this via PHP?


Well, you could, but it's more efficient to do this in the Apache config. However, if you needed to do something more complex then maybe PHP would be better. For instance, by testing ^([a-z]{2}) you are only checking the first language (which you assume is the preferred language). However, the Accept-Language header is quite complex as it can contain multiple languages with different weights (eg. Accept-Language: fr-CH, fr;q=0.9, en;q=0.8, de;q=0.7, *;q=0.5). If you needed to parse this header then PHP would be preferable.


Or for SEO is better use htaccess?


For SEO it makes no difference.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme