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.
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]
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]
Related
in .htaccess Am using :
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1/$1/ [R=301,L]
It works fine for main domain i.e. redirects example.com to https://www.example.com
but not works on sub pages like
site.com/blog not redirects to https://www.example.com/blog
also like to add tailing slash '/' after url
https://www.example.com/blog/
how to achieve this?
Thanks in advance!
You should have htaccess rule like this. Make sure you keep these rules at the top of your htaccess file. Please clear your browser cache before testing your URLs.
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI}/ [NE,R=301,L]
I have a website, let's say www.example.com but this used to be www.example.nl. All traffic from www.example.nl is now redirected to www.example.com, but as we changed some naming conventions, www.example.nl/seeds now has to redirect to www.example.com/nl/flower-seeds.
The .htaccess file I got contains the following code:
RewriteEngine On
RewriteBase
RewriteCond %{HTTP_HOST} !example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L, R=301]
Redirect 301 /seeds/(.*) example.com/nl/flower-seeds/$1
When I navigate to www.example.nl/seeds/ I end up at www.example.com/seeds/ which ends up in a 404 because I'm missing the /nl/flower- part.
I think Redirect doesn't work properly when the URL is already altered by a RewriteRule. How would you tackle this problem? Any help is appreciated!
You should exclude /seeds/ directory form general rules like this :
RewriteEngine On
RewriteCond %{HTTP_HOST} !example.com$ [NC]
RewriteCond %{REQUEST_URI} !/seeds/(.*)
RewriteRule ^(.*)$ http://www.example.com/$1 [L, R=301]
RewriteCond %{HTTP_HOST} !example.com$ [NC]
RewriteRule ^seeds/(.*)$ http://www.example.com/nl/flower-seeds/$1 [L, R=301]
Note: clear browser cache then test
Also , don't use regex with redirect like what you did :
Redirect 301 /seeds/(.*) example.com/nl/flower-seeds/$1
Here this (.*) has no meaning , you could use either RedirectMatch or RewriteRule
I have a website on a domain that I want to move to another domain. Here is my problem. The root URL has to redirect to a different location on the new domain. Example:
http://subdomain.olddomain.com/ must redirect to http://www.newdomain.com/location
http://subdomain.olddomain.com/code/slug must redirect to http://www.newdomain.com/slug
The htaccess code that I have doesn't work ver
RewriteRule ^/?(.*) http://www.newdomain.com/$1 [R=302,L]
This code works for the second example, but nog on the first. Can anyone help me setup the correct htaccess code?
Try:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^subdomain\.olddomain\.com$ [NC]
RewriteRule ^code/(.*)$ http://www.newdomain.com/$1 [L,R]
RewriteCond %{HTTP_HOST} ^subdomain\.olddomain\.com$ [NC]
RewriteRule ^$ http://www.newdomain.com/location [L,R]
I have a Magento website that is moving to a new domain. Im looking to 301 redirect all pages from the old domain to the new domain keeping same url structure.
I've updated the .htaccess file on my old domain with:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^new-domain.com$ [NC]
RewriteRule ^(.*)$ http://new-domain.com/$1 [R=301,L]
The issue: The above seems to redirect every instance of: old-domain.com/subdir/whatever only just to the main domain: new-domain.com.
I'd be looking to redirect old-domain.com/subdir/whatever to: new-domain.com/subdir/whatever.
Any idea on what could be wrong?
Adjust your rule to use REQUEST_URI variable which is not dependent on the directory of .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?old-domain\.com$ [NC]
RewriteRule ^ http://new-domain.com%{REQUEST_URI} [R=301,L,NE]
Better to clear your browser cache before testing this rule.
PS: You need to match hostname condition to old-host not the new-host.
This should redirect and retain the path after the main domain.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^old-domain\.com$ [NC]
RewriteRule ^(.*)$ http://new-domain.com/$1 [R=301,L]
I have written a simple redirect condition as:
RewriteCond %{HTTP_HOST} !^my-domain\.com$
RewriteRule ^(.*)$ http://my-domain.com/hu/$1 [L,R=301]
It redirects correctly from www.mysite.com to mysite.com/hu/
But it does not redirect mysite.com to mysite.com/hu/
Please help
You've cleary copied this code without understanding it. This is a typical htaccess to remove the www. part of a domain.
To redirect your homepage to a subfolder, use this code instead :
RewriteCond %{HTTP_HOST} ^(www\.)?my-domain\.com$
RewriteCond %{REQUEST_URI} !^hu/$
RewriteRule ^(.*)$ http://my-domain.com/hu/$1 [L,R=301]