force redirect if the .htaccess rule is met

1

On my website, I use languages in this way

domain.com/es/
domain.com/br/
domain.com/en/
etc ...

I want to know the form with "htaccess" to redirect the url forsozamente, if the user enters this way domain.com /

force him to go to: domain.com/en/

    
asked by John 28.06.2016 в 15:50
source

3 answers

3

If the idea is simply to redirect the root domain, then that htaccess section should look like this:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^dominio\.com$
RewriteRule ^$ es [L]
    
answered by 28.06.2016 в 16:09
0

Why use a .htacces to address a single address? It would be enough to put a index.html (or the extension that corresponds) in the root ( dominio.com/index.html ) with a refresh that makes the redirect.

Doing it with .htacces is a waste, because that file would be parsed for each query to the site (and it is not cached).

    
answered by 28.06.2016 в 16:33
0

to work with any domain, just put a rule that is "In the case that does not start with any language, redirects to / is /"

RewriteEngine on
RewriteRule ^(?!es|en|br)(.*) /es/$1/$2 [R=301,L]
# RewriteRule ^(es|en|br/)(.*) $1$2 [R=301,L]

The second rule is commented, it is just the opposite and leave it just in case.

It would be good to also pass in the options, the option QSA that indicates that if a query string comes, it is appended to the URL. But this already depends on your application

PS: tested with Htaccess Tester

    
answered by 28.06.2016 в 17:43