1

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

5
  • We cannot tell you way your SEO is bad. There are so many possible reasons therefore this is the wrong place to discus it. Oct 5, 2017 at 0:03
  • 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 why Oct 5, 2017 at 0:06
  • 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
    Oct 5, 2017 at 7:09
  • You used my answer only with hard code domain... Oct 7, 2017 at 11:57
  • Yes i use your code but i need more points (reputation) to vote so i can't sorry..
    – Lorenzo
    Oct 9, 2017 at 6:54

2 Answers 2

2

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>
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
2
  • Yes you right, but if you have http:// www. example.com this will end up in 2 redirects first from non-www to www and than from http to https. Oct 5, 2017 at 12:07
  • Thanks for code : but the homepage example.com does not work : connection not possible, maybe a loop ... when i change your code , i can't display homepage but /es/ is not added /es/ and /es are the same , i really need to redirect /es -> /es/ like dafont.com/es -> dafont.com/es/ make it (it's not my website).Thanks.
    – Lorenzo
    Oct 6, 2017 at 6:47
0

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

5
  • Thanks for simplification.Only last rewrite condition does not work. Forbidden : You don't have permission to access /index.php on this server. i still need to force /lg to /lg/ is it correct ? : RewriteRule ^(en|it|pt|esp)\/?$ index.php?lg=$1 [QSA,NC,L]
    – Lorenzo
    Oct 5, 2017 at 7:09
  • and example.com/lg/text not possible just example.com/lg/
    – Lorenzo
    Oct 5, 2017 at 7:21
  • Ok, i've made multiple test and it's not working : some rules are ok www-> non www , http -> https , but example.com/es/ -> 404 error , example.com/es -> still not redirect to example.com/es/ it's not my website but i want something like dafont.com/es -> www.dafont.com/es/ (slash is added always) www.dafont.com/es.php -> 404 . I just need 2 letters index.php?lg=es not index.php?lg=es&p=home so just $1 as a rule. Redirect index.php?lg=es -> /es/ even if more parameters are added. Thanks again
    – Lorenzo
    Oct 6, 2017 at 6:45
  • Ok, i know that cache need to be erase :) i've made a little mix and now it works : /it -> redirect to /it/ -> cool and index.php?lg=it -> You don't have permission to access /index.php on this server. The beginning of your code leads to an error for homepage so here is all the code : see on my first post on top
    – Lorenzo
    Oct 7, 2017 at 8:26
  • Ok, i try to vote for your solution but i need more points (reputation) to vote so i can't sorry...
    – Lorenzo
    Oct 9, 2017 at 6:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.