Willy
Willy

Reputation: 11

Htaccess redirect subdomain doesn't work

I have a subdomain called es and I need when someone wants to enter mysite.com/es it can be redirect to es.mysite.com. It works with the following htaccess:

RewriteEngine On  
RewriteCond %{HTTP_HOST} !^www.mysite.com$ [NC] 
RewriteRule ^(.*)$ http://es.mysite.com/$1 [R=301,L]  
RedirectMatch permanent ^/es/?$ http://es.mysite.com/$1

The problem is when someone types mysite.com/es/bla/bla/bla. In this case, with the current configuration on my htaccess, the user isn't redirected and I want the user can be redirected.

For example:

If I enter:

http://letsbonus.com/es/barcelona/spa-experiencie-para-2-opcion-masaje-desconecta-roc-nature-273710

This is redirect to:

http://es.letsbonus.com/barcelona/spa-experiencie-para-2-opcion-masaje-desconecta-roc-nature-273710

Thanks in advance.

Upvotes: 1

Views: 169

Answers (2)

anubhava
anubhava

Reputation: 785128

You need just this one rule in your root .htaccess of mysite.com:

RewriteEngine On  
RewriteCond %{HTTP_HOST} ^(www\.)?(mysite\.com)$ [NC] 
RewriteRule ^es/(.*)$ http://es.%1/$1 [R=301,L,NE]

Upvotes: 1

zessx
zessx

Reputation: 68790

Your two rules enter into conflict:

  • Base url: http://example.com/es/test
  • Non-www redirection -> http://www.example.com/es/test
  • es subdomain redirection -> http://es.example.com/test
  • Non-www redirection... (we're no longer under www subdomain)

I would use this htaccess to get the excepted result:

RewriteEngine On  

RewriteCond %{HTTP_HOST} !^(www|es).example.com$ [NC] 
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L] 

RewriteRule ^es/(.*)$ http://es.example.com/$1 [R=301,L]  

Upvotes: 0

Related Questions