1
votes

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

2
We cannot tell you way your SEO is bad. There are so many possible reasons therefore this is the wrong place to discus it.Webdesigner
What should happen if we have a URL like example.com/it/some-page ... And example.com/it/ and example.com/it should deliver the same page IMHO, if not please explain whyWebdesigner
Thanks for reply, i think example.com/it/ and example.com/it is maybe consider as a duplicate content ? on many website /it redirect to /it/Lorenzo
You used my answer only with hard code domain...Webdesigner
Yes i use your code but i need more points (reputation) to vote so i can't sorry..Lorenzo

2 Answers

0
votes

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

1
votes

This solution is nice because can be applied to any domain (no hard code names).

Some notes:

  1. Remove www. (no domain name hardcoded)
  2. Force https (in dev environment you can work with http)
  3. Add allways a slash
  4. Define your default homepage if page name isn't specified
  5. Mapping language and page from URL to (internal) query string

Be aware that 4 and 5 have the index.php file in public/ folder...change or remove it if you want.


# ###############################################################################
# | Rewrite engine  (SEO URLs friendly)                                            |
# ###############################################################################
<IfModule mod_rewrite.c>

    RewriteEngine On
    Options +FollowSymlinks

    ## Process pages requests

  # 1. To redirect from www to non www (Rewrite www.example.com → example.com)
    RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
    RewriteRule ^(.*) https://%1/$1 [R=301,NE,L]

  # 2. Redirect HTTP to HTTPS automatically (only if not in localhost)
    RewriteCond %{HTTP_HOST} !=localhost
    RewriteCond %{HTTPS} off
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

  # 3. (Add a slash) aa → aa/
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([^./]+)$ $1/

  # 4. (Default homepage) aa/ → ?lang=aa&page=home
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([^./]+)/$ public/index.php?lang=$1&page=home

  # 5. (Language and page) aa/bb → ?lang=aa&page=bb
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([^./]+)/([^./]+)$ public/index.php?lang=$1&page=$2

</IfModule>
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -