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

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]

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.

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]

htaccess redirect from domain1 to domain2

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.

Why won't my mod_rewrite work for HTTPS like HTTP

I'm sorry for yet another of these posts, but from what I can tell, my question is different than the prevailing http -> https redirects out there.
I want to
redirect all http://www.mydomain.com traffic to https://www.mydomain.com/wiki
AND
redirect https://www.mydomain.com to https://www.mydomain.com/wiki
Notice the https in my first redirect goal.
For the first redirect, I am able to accomplish this by putting:
RewriteEngine On
RewriteCond %{REQUEST_URI} /
RewriteRule ^(.*) https://%{SERVER_NAME}/wiki [R,L]
In my httpd.conf file and restarting.
I thought this would also work for my https:// attempts. Notice, I am not including
RewriteCond %{HTTPS} !=on
or anything like that. Yet, still https://www.mydomain.com sends me to my index.html file in my server root.
If I try to put the above Rewrite directives in my httpd-ssl.conf file and restart the server then I get infinite redirects.
What am I doing wrong?
NOTE: For what its worth, /wiki is an alias to /home/Users/myusername/www/wiki (the absolute path to wiki)
UPDATE
Rehash as to what I've tried so far:
Attempt 1:
In httpd.conf:
RewriteEngine On
RewriteCond %{REQUEST_URI} /
RewriteRule ^(.*) https://%{SERVER_NAME}/wiki [R,L]
In httpd-ssl.conf:
Nothing Rewrite related
Result 1:
Redirects all http traffic to https://www.mydomain.com/wiki
Does nothing for https://www.mydomain.com
Attempt 2:
In httpd.conf:
RewriteEngine On
RewriteCond %{REQUEST_URI} /
RewriteRule ^(.*) https://%{SERVER_NAME}/wiki [R,L]
In httpd-ssl.conf:
RewriteEngine On
RewriteCond %{REQUEST_URI} /
RewriteRule ^(.*) https://%{SERVER_NAME}/wiki [R,L]
Result 2:
Infinite redirects.
Attempt 3:
In httpd.conf:
RewriteEngine On
RewriteCond %{REQUEST_URI} /
RewriteRule !^wiki https://%{SERVER_NAME}/wiki [R,L]
In httpd-ssl.conf:
RewriteEngine On
RewriteCond %{REQUEST_URI} /
RewriteRule !^wiki https://%{SERVER_NAME}/wiki [R,L]
Result 3:
Infinite redirects.
Your rule will match every request (even /wiki). Try to exclude that:
RewriteRule !^wiki https://%{HTTP_HOST}/wiki [R,L]
The infinite redirects are coming from the fact that the rule matches both the first client request (eg. http://...) and from any subsequent request.
You would do better to use a rewrite rule which excludes the wiki path from the match e.g.
RewriteRule !^wiki https://%{HTTP_HOST}/wiki [R,L]
or you might get better performance from a RewriteCond
RewriteCond %{REQUEST_URI} !^wiki
before your existing rule. This will make it easier to add more complex patterns that you currently have in your example (should you need to later in your project).
I found a solution that might not be the most robust but it works for me.
In my httpd-ssl.conf file, under the VirtualHost container, I set the DocumentRoot to point to the directory that I've been trying to redirect to.
<VirtualHost _default_:443>
DocumentRoot "path on my HD where I wanted to redirect all along"
ServerName All the usual stuff...
...
This takes care of "redirects" (not really redirects anymore) for https transactions
For http (non-ssl) I placed in httpd.conf:
RewriteEngine On
RewriteCond %{REQUEST_URI} /
RewriteRule ^(.*) https://%{SERVER_NAME}/wiki [R,L]
That seemed to do it.

Apache: redirecting users, but keep same path?

I want to be able to redirect users to a different TLD but keep the same path:
For example if the user goes to:
example.com/cars/10
Using apache how can I redirect the user to something like:
my_new_site.com/cars/10
If you have mod_rewrite enabled on your server, you can place this into your .htaccess file.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule ^(.*)$ http://my_new_site.com/$1 [R=301,L]
</IfModule>
use a 302 redirect in your config:
<VirtualHost *:80>
ServerName example.com
Redirect /cars http://my_new_site.com/cars/
</VirtualHost>
If you need more flexibility, you can use mod_rewrite, and then use those rewrites:
RewriteEngine on
RewriteRule ^/(.*)$ http://my_new_site.com/$1 [NC]
There's a nice documentation at apache.org.