Saturday, 9 September 2017

php - Multiple languages + Htaccess



I need for a domain to manage languages (en/it/pt/es) plus remove www. and force https




RewriteEngine On
# -> www.example.com to example.com
RewriteCond %{HTTP_HOST} www.example.com
RewriteRule (.*) http://example.com/$1 [L,R=301]

# -> http -> https
RewriteCond %{SERVER_PORT} 80
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]


I don't know how to make this rewrite rule :
index.php?lg=es -> /es/ and not /es or /es.php and even refuse index.php?lg=es



RewriteRule ^([a-zA-Z0-9]+)/?$ index.php?lg=$1



??



The SEO is bad for my website only english is known by engine and others languages are on the 4th page of result..



Is it the reason why because i've got two ways to display url ??



example.com/it/ and example.com/it should not return the same page



Thanks for help.




EDIT (i think all is correct ?? ) : 07 October thanks for all answers :




RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com$ [OR]
RewriteCond %{SERVER_PORT} 80 [OR]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
RewriteRule ^/?([a-z]{2})$ /$1/ [R=301,NC,L]
RewriteRule ^/?([a-z]{2})/$ /index.php?lg=$1 [NC]

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_URI} ^/?index.php$
RewriteCond %{QUERY_STRING} ^lg=([^&]+)(&.*)?$
RewriteRule ^/?index.php$ - [F]
ErrorDocument 404 /404.php


Just a question : Is testing if https off really useful (slow connection ?) ? thanks



EDIT 8 February 2018




After months of use there are a lot of 404 errors .
Htaccess need to more simplified.



I just need :



 1) www -> non-www
2) http -> https
3) index.php -> example.com
4) redirect index.php?lg=it and index.php?lg=es and index.php?lg=pt

to example.com/it or to example.com/es or to example.com/pt


and if it's not one of this language -> go to homepage example.com or 404 ?? but Google webmaster will increase 404 error ...



I need a very simplify version of htaccess with 3 languages with Rewrite Base in it ? or not ?



here is my htaccess :



RewriteEngine On

RewriteCond %{HTTP_HOST} ^www.example.com$ [OR]
RewriteCond %{SERVER_PORT} 80 [OR]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
RewriteRule ^/?([a-z]{2})$ /$1/ [R=301,NC,L]
RewriteRule ^/?([a-z]{2})/$ /index.php?lg=$1 [NC]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_URI} ^/?index.php$
RewriteCond %{QUERY_STRING} ^lg=([^&]+)(&.*)?$
RewriteRule ^/?index.php$ - [F]

ErrorDocument 404 /404.php


Everything is managed in the index.php, is it better to create 3 pages ?



index.php -> redirect to example.com



es.php -> redirect to example.com/es



it.php -> redirect to example.com/it removing .php




Can someone make it ? Thanks


Answer



This should work for you as accepted:



RewriteEngine On
# redirect to none-www and https
# if host do not starts with www.
RewriteCond %{HTTP_HOST} ^www\.(([A-Za-z0-9][A-Za-z0-9-]+\.)+[A-Za-z]{2,})$ [OR]
# or if Port is 80

RewriteCond %{SERVER_PORT} 80 [OR]
# or if https is off
RewriteCond %{HTTPS} off
# redirect to example.com with https
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

# Redirect Language Strings without Trailing Slashes
RewriteRule ^/?([a-z]{2})$ /$1/ [R=301,NC,L]

# Rewrite Language string

RewriteRule ^/?([a-z]{2})/$ /index.php?lg=$1 [NC]

# Prevent direct access to /index.php?lg=$1 [QSA,NC,L]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_URI} ^/?index.php$
RewriteCond %{QUERY_STRING} ^lg=([^&]+)(&.*)?$
RewriteRule ^/?index.php$ - [F]


I tested this positive an Ubuntu 16.04.03 LTS with Apache 2.4.27



No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...