Redirect request to another domain with mod_rewrite - apache

Is it possible in Apache to redirect a request from my domain: www.example.com/index.html to another domain http://www.other-domain.com/page/info as if I was directly visiting that page?
So the user shouldn;t notice anything. He should simply think he's visiting www.example.com, while apache serves back the content of http://www.other-domain.com/page/info.
This is because the other domain is the new server, and the users should be able to visit the old URL as well. Note, I dont want to do a 301 redirect.
I know this can be done with VirtualHost, but my web host doesn't allow me to use that. I can use mod_rewrite, so I'm hoping I can do the same trick with that.
Anyone any idea if t his is possible, and if so, how to do it?
RewriteEngine on
RewriteRule ^/index.html$ http://www.other-domain.com/page/info [PT]

Only way to achieve this is by enabling mod_proxy on www.example.com. Once mod_proxy and mod_rewrute are enabled place this rule in DocumentRoot/.htaccess of www.example.com:
RewriteEngine On
RewriteRule ^/?(index\.html)?$ http://www.other-domain.com/page/info [L,P]

Related

Redirection https to https within same Apache

I have a requirement where I have to redirect my hostname to particular application which is again hosted on same Apache. Let's take an example, When I hit on host(https://domain1.example.com), It should internally redirect me to Apache Web Application (https://domain1.example.com/application1) without changing the browser URL.
I am not sure how to achieve SSL to SSL redirection. Thanks in Advance..!!!
This should work. This will redirect all incoming urls that are going to domain1.example.com/ to domain1.example.com/application1
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain1.example.com$
RewriteRule ^$ https://domain1.example.com/application1 [L,R=301]
If without changing browsing URL is your goal then PROXY is your way.
Put following in your apache vhost or global file,
ProxyPass https://domain1.example.com/ https://domain1.example.com/application1
ProxyPassReverse https://domain1.example.com/ https://domain1.example.com/application1
PS shibboleth has nothing to do with this, at least you have not mentioned any case.
EDIT
ProxyPass should come to virtural host not in location
Ideally all the location tag should be out of virtual host

Rewrite Apache directive for a 301 redirect

There is a malicious site which has replaced mine in Google search results. I checked the headers and this malicious domain is sending traffic directly to our servers.
I'd like to rewrite Apache's directives so that there is a 301 redirecting any traffic from the malicious site to our site in order to get Google to give me back my listing.
Does anyone know how to write this directive? I hardly know anything about this stuff but I think it involves RewriteEngine. Do I update it in .htaccess?
UPDATE FOR CLARITY: When someone searches for my company name in Google the malicious domain is the top result. Click on it and it immediately goes directly to our server - it's not a redirect, it's a straight 200 to my IP (but in their domain). Makes me think they're pointing their DNS to my IP. Is there a way to write a directive that would execute a 301 redirect to my domain whenever traffic was coming from the malicious domain?
RESOLVED: I had help figuring this out... here's the fix. I put this at the top of my {site.com}.conf file:
<VirtualHost *:80>
RedirectMatch permanent ^/(.*) http://www.{site}.com/$1
</VirtualHost>
You say this:
I checked the headers and this malicious domain is sending traffic
directly to our servers.
Then this:
I'd like to rewrite Apache's directives so that there is a 301
redirecting any traffic from the malicious site to our site in order
to get Google to give me back my listing.
So it’s already sending traffic to your servers but you want to do what exactly? Apache rewrite rules only work on servers you have control of. If someone poisoned your listings on Google & now gets traffic, your access to your own servers won’t change that.
I had help figuring this out... here's the fix. I put this at the top of my {site.com}.conf file:
<VirtualHost *:80>
RedirectMatch permanent ^/(.*) http://www.{site}.com/$1
</VirtualHost>
test the host header and redirect.
RewriteEngine on
RewriteCond %{HTTP_HOST} !^(www\.)?yourhost.com$
RewriteRule . http://www.yourhost.com%{REQUEST_URI} [R=301]

Subdomain forward to another subdomain without redirecting

I've spent an amout of time to try solve it, and finally without any effects.
I have a casual subdomain like test.example.com and a mobile subdomain m.test.example.com. Every subdomain has own directory test (I have my scripts here) and m.test.
I need to forward users from m.test.example.com to test.example.com without redirecting to test.example.com.
I know it easy to do it by Virtual Hosts or cpanel, but I need to do it on shared hosting with DirectAdmin (user level only). I think about mod_rewrite, and I also tried PHP symlink(), but it is disabled on my hosting.
How can I forward the user from the mobile subdomain to non-mobile resources?
Since the two domains ("test" and "m.test") have different document roots, you're going to need to reverse proxy. The problem here is mod_proxy, the proxy module, may not be loaded by your host, but you'd do something like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^m\.test\.example\.com$ [NC]
RewriteRule ^(.*)$ http://test.example.com/$1 [L,P]
These rules would be in the /m.test/ folder, the document root of the m.test.example.com subdomain. The P flag tells mod_rewrite to hand the request to mod_proxy to reverse proxy instead of redirecting the browser.

Changing the URL within .htaccess

I'll try and explain this the best I can; I have two domain names: www.original.com and www.mysite.com/. Within the site for www.original.com there is no content and all the content is hosted in www.mysite.com/original/. Now, when I visit www.original.com I have set up a redirect like this:
redirectMatch 307 ^(.*)$ http://www.mysite.com/original
However, I have realised that this is not what I need, because I would like the visitors of the www.original.com site to see www.original.com on the address bar, and not http://www.mysite.com/original. Is there any way to do this in .htaccess? I used to do it in the cPanel of my old hosting account via an addon domain, but the hosting I have now (united-domains.de) do not provide addon domains, apparently.
Thanks
Since the 2 sites are separate, you'll need to rely on mod_proxy. There are a number of ways to create a reverse proxy from the www.original.com domain to the www.mysite.com domain. The easiest would be to use ProxyPass and ProxyPassReverse:
ProxyPass / http://www.mysite.com/original
ProxyPassReverse / http://www.mysite.com/original
But these are only applicable inside the server or vhost config. If you don't have access to that, you'll have to use mod_rewrite's [P] flag, which hands the target substitution of a RewriteRule to mod_proxy, and you can use this within an htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?original\.com$ [NC]
RewriteRule ^(.*)$ http://www.mysite.com/original/$1 [L,P]
You have to tell www.original.com to act like a proxy server. but this may require you more than just editing an htaccess.
See this for more informations : http://httpd.apache.org/docs/2.2/mod/mod_proxy.html

.htaccess: Redirect depending on accessed url

I want to, in my .htaccess, redirect the user to another url depending on what the user accesses.
In this case, http://example.com/awesome.com and http://awesome.com is the same site, and if the user is accessing http://example.com/awesome.com, I want him or her to be redirected to http://awesome.com.
Is this feasible?
Edit: With the help of answers, I came up with this working solution:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^awesome.com$
RewriteRule ^(.*)$ http://awesome.com/$1 [R=301]
you can use mod_rewrite (apache2 module)
this is the .htaccess that i use in order to redirect from my old domain to my new one (while keeping the link strcture e.g www.domain1.com/link/linkb.html becomes www.domain1.gr/link/linkb.html)
RewriteEngine On
RewriteCond %(www\.)?domain1\.com$ [NC]
RewriteRule .* http://www.domain1.gr%{REQUEST_URI} [R=301,L]
google mod_rewrite for more information (syntax etc)
Not entirely sure about .htaccess, but you could just use server code on your 404 page to redirect them appropriately; this way you could collect stats, setup a toolbar, or whatever other actions you might want to take.
.htaccess is about authorization, not redirection. I recommend you look at the redirection support for Apache (or whatever web server you're using), which is a much better fit for this problem and just make sure your .htaccess/authorization is in line with the target.
This rule should do it:
RewriteRule ^awesome\.example(/.*)?$ http://www.awesome.example$1 [R=301,L]
Check the Redirect & RedirectMatch options in apache. For simple cases, like yours it's simplier than a mod_rewrite.
Redirect /awesome.com http://ww.awesome.com
or
Redirect permanent /awesome.com http://ww.awesome.com
Now, if example.com and awesome.com are on the same apache server and same virtualhost you're maybe mising the named bases virtualhost things and you're maybe trying to make something really more complex than a simple named base virtualhost definition.