.htaccess language redirects with SEO-friendly URLs

.htaccess language redirects with SEO-friendly URLs - If a page has internal and external outgoing links to redirecting URLs, it’s returning 3xx (301, 302, etc.) HTTP status codes standing for redirection. This issue means that the page does not exist on a permanent or temporary basis. It appears on most of the popular web browsers, usually caused by a misconfigured website. However, there are some steps you can take to ensure the issue isn’t on your side. You can find more details about redirecting URLs by reading the Google Search Central overview. In this article, we’ll go over how you can fix the .htaccess language redirects with SEO-friendly URLs error on your web browser. Problem :


How do I setup my .htaccess file to detect several languages, and redirect them to specific SEO-friendly URLs?



Basically every URL needs to go to index.php?lang=(...).



So, for English language detection http://mysite.com has to go to http://mysite.com/en/ (index.php?lang=en).



My .htaccess as of now (not working):



RewriteEngine On

RewriteCond %HTTP:HOST http://mysite.com/

RewriteCond %HTTP:Accept-Language ^en [NC]
RewriteRule ^$ http://mysite.com/en/ [L,R=301]

RewriteCond %HTTP:Accept-Language ^de [NC]
RewriteRule ^$ http://mysite.com/de/ [L,R=301]

RewriteCond %HTTP:Accept-Language ^nl [NC]
RewriteRule ^$ http://mysite.com/nl/ [L,R=301]

RewriteCond %HTTP:Accept-Language ^fr [NC]
RewriteRule ^$ http://mysite.com/fr/ [L,R=301]

RewriteCond %HTTP:Accept-Language ^es [NC]
RewriteRule ^$ http://mysite.com/es/ [L,R=301]

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

RewriteRule ^(en|de|nl|fr|es)$ index.php?lang=$1 [L,QSA]

Solution :

You're almost there. Drop the very first RewriteCond (as it's not required and won't match ever) and add a trailing / to your last RewriteRule since you're redirecting with them.



RewriteEngine On

RewriteCond %HTTP:Accept-Language ^en [NC]
RewriteRule ^$ http://mysite.com/en/ [L,R=301]

RewriteCond %HTTP:Accept-Language ^de [NC]
RewriteRule ^$ http://mysite.com/de/ [L,R=301]

RewriteCond %HTTP:Accept-Language ^nl [NC]
RewriteRule ^$ http://mysite.com/nl/ [L,R=301]

RewriteCond %HTTP:Accept-Language ^fr [NC]
RewriteRule ^$ http://mysite.com/fr/ [L,R=301]

RewriteCond %HTTP:Accept-Language ^es [NC]
RewriteRule ^$ http://mysite.com/es/ [L,R=301]

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

RewriteRule ^(en|de|nl|fr|es)/?$ index.php?lang=$1 [QSA,NC,L]


Here is my code for language and country parameters. This supports any language:



#www.country.com/index.html  becomes www.country.com/index.php
#www.country.com/fr/index.html becomes www.country.com/index.php?language=fr
#www.country.com/ca/fr/index.html becomes www.country.com/index.php?country=ca&language=fr

RewriteRule ^([a-zA-Z]2)/([a-zA-Z]2,3)/index.html$ index.html?country=$1&language=$2
RewriteRule ^([a-zA-Z]2,3)/index.html$ index.html?language=$1
RewriteRule ^index.html$ /index.php [L]

We hope that this article has helped you resolve the htaccess, mod-rewrite, redirects error in your web browsers. Enjoy browsing the internet uninterrupted!

Comments

Popular posts from this blog

How to redirect to any domain [duplicate]

"302 found" for index page bad for SEO?

Create redirect from url like www.example.us/?p=100&option=