htaccess redirect from domain1 to domain2 - apache

I know, this should be easy, but I cannot make it work.
I have 2 domains pointing to the same folder in an Apache server, bahiadivers.com and bahiadivers.cl.
I want to redirect bahiadivers.cl to bahiadivers.cl/es/ and change the address bar.
I try this (among a million things)
rewritecond %{HTTP_HOST} ^bahiadivers.cl [nc]
rewriterule ^/$ http://www.bahiadivers.cl/es/$1 [r=301,nc]
but didn't work... I mean, the URL is not changing in the browser address bar, but also the languge (/es/) is not working... how should I do this?
Thanks!

Try this for your .htaccess
# Apache configuration file
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine on
# If URL is www.example.com/es/Blah, use /Blah/
# Prevents needing to change existing links.
RewriteBase /es/
# Rewrite www.example.com to example.com
RewriteCond %{HTTP_HOST} ^www.bahiadivers.cl
RewriteCond %{HTTP_HOST} ^bahiadivers.cl
# Rewrite any inbound URL Bits to http://bahiadivers.cl/es/...
RewriteRule (.*) http://bahiadivers.cl/es/$1 [R=301,L]
</IfModule>
That should do it.

Related

.htaccess rewrite to redirect domain.com to domain.net/subdirectory

I would like to redirect somedomain.tv to anotherdomain.com/tv and (if possible) keep the URL as somedomain.tv
My .htaccess so far:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^radiouas\.tv$ [OR]
RewriteCond %{HTTP_HOST} ^www\.radiouas\.tv$
RewriteRule ^/?$ "http\:\/\/www\.radiouas\.org\/tv" [R=301,L]
</IfModule>
# This simply redirects but does not keep the original URL
I have found many ways to do that when you have only one domain (e.g. domain.com to domain.com/blog). But in my case, I'm using two different domains...
How can I achieve that? Thanks in advance.

.htaccess old domain to a new domain not working when structure page IS NOT the same

I have this on my .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^(.*)$ http://www.houstonhouseandhome.net/$1 [R=301,L]
</IfModule>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www.houseandhomeonline\.com$ [NC]
RewriteRule ^(.*)$ http://www.houstonhouseandhome.net/ [R=301,L]
The homepage goes to the new domain, but the subpages of the old domain are not going to the index of the new domain. they go to the new domain and look for the very same page, is there a way that the requests from the old domain pages go to the new domain index?
Thank you,
For some reason you've got two sets of rules.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^(.*)$ http://www.houstonhouseandhome.net/$1 [R=301,L]
</IfModule>
And then
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www.houseandhomeonline\.com$ [NC]
RewriteRule ^(.*)$ http://www.houstonhouseandhome.net/ [R=301,L]
You only need the first set. The [L] flag tells apache to stop processing any more rules if this one matches. Since it will match everything, the second set of rules will never take effect.
To to stop everything trying to find a relative page on the new domain, you simply remove the $1 from the end of the domain. So your htaccess file will look like this:
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^(.*)$ http://www.houstonhouseandhome.net/ [R=301,L]
</IfModule>
Note
Since you're using 301 redirects, make sure your browser has cleared its cache/history before retesting. Browsers will not request an url it thinks will respond with a 301.

Rewrite domain.com/sub/ to sub.domain.com on other server

I see many questions about rewriting from sub.domain.com to a local domain.com/sub/ folder, but have not found any for a rewrite in the other direction.
Keep in mind sub.domain.com is not on the same server as domain.com.
When a user goes to domain.com/sub/, that must actually be pointing them to sub.domain.com without a redirect.
Is this possible?
On domain.com enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^sub(/.*|)$ http://sub.domain.com$1 [L,R=301,NC]
UPDATE
As per the comments if you don't want original URL to change: This will require you to enable mod_proxy on domain.com:
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^sub(/.*|)$ http://sub.domain.com$1 [L,P,NC]

Redirect subdomain to another subdomain

i want to know how could i redirect subdomain to another subdomain and also different domain name
for example
i want to redirect subdomain
forum.example.com
to
forum.example.net
thanks
This code should work for you. you need to add a redirect to your .htaccess file.
Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^example.com [nc]
rewriterule ^(.*)$ http://www.example.net/$1 [r=301,nc
Another good, but lazy way is to use the following link, to genereate the code for you htaccesss generator
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^forum\.example\.com$ [NC]
RewriteRule ^ http://forum.example.net%{REQUEST_URI} [R,L]

Domain .htaccess redirect issue

I'm trying to create some redirects with .htaccess but I never manage to get it fully functional. Maybe someone here can help me.
What I need is:
http://domain.se and http://domain.com to redirect to http://www.domain.com.
I also need http://domain.se/somefolder, http://domain.com/somefolder as well as http://www.domain.se/somefolder to redirect to http://www.domain.com/folder.
I've tried to accomplish this myself but all I end up with is errors about data not being sent.
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
# folder rewrite
RewriteRule ^somefolder$ folder [L]
# domain redirect
RewriteCond %{HTTP_HOST} =domain.com [OR]
RewriteCond %{HTTP_HOST} =domain.se
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]
This is to be placed in .htaccess file in website root folder. If placed elsewhere some tweaking may be required.
First rule will rewrite (internal redirect) requests to /somefolder to /folder. If you need this to be 301 Permanent Redirect, then replace [L] by [R=301,L]
Second rule will do domain redirect job. This rule will ONLY redirect if domain is domain.com or domain.se. If you want to have redirect from ANY domain name (that your webserver is configured can serve) to www.domain.com then replace those 2 RewriteCond lines with this one: RewriteCond %{HTTP_HOST} !=www.domain.com.
RewriteCond %{HTTP_HOST} !^www.domain.com$
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301]
That should meet all of your requirements. All requests that are not www.domain.com will be redirected to that domain, with the request URI intact.