Subdomain forward to another subdomain without redirecting - apache

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.

Related

Using htaccess, Redirect http or https domain, and any subpage to particular page on another server

Background:
We have DNS pointing to new server.
Apache Server has a directory called example
Example directory has an htaccess file for redirecting, code below
Issue is : We need this to redirect any variation of the url ie: http://example.com, https://example.com, http://example.com/filename.html, https://example.com/directory/filename.html, etc.. to a specific page on another server.
So far, we have the code below, it works correctly for https, but not http.
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ https://differenturl/login/login.html [R=301,L]
I feel like there is a simple answer but can't seem to find it!
**Edit: for clarification -by not working, I mean that I am not redirected to https://differenturl/ etc... using http. It only works for https. http routes me to the home page of the server that this all sits on. And that server has a different domain name than anything above.
If it's "not working" for HTTP then either:
The <VirtualHost> container for port 80 (ie. HTTP) is not configured at all in the server config.
Or,
The vHost:80 container is pointing to a different area of the filesystem, so the .htaccess file is not processed.
Or,
The vHost:80 container does not permit .htaccess overrides so the .htaccess file is ignored. You either need to configure .htaccess overrides by setting AllowOverride All in the appropriate <Directory> container in the vHost:80 container. Or simply redirect everything to HTTPS in the vHost:80 container (eg. Redirect / https://example.com/) and then allow the .htaccess file to redirect from HTTPS. (That's potentially 2 redirects, but that should not be an issue.)
Or just do the redirect (to the other server) in the server config and not use .htaccess at all.

Redirect request to another domain with mod_rewrite

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]

Rewrite address bar without redirecting

I have a host in my university's http server under the domain: university.com/~username
Now, I also have a domain on: mydomain.net with GoDaddy
I want to know if it is possible to have a subdomain:
university.mydomain.net that basically redirects to
university.com/~username.
Now, the trick here is that if I want to access
university.com/~username/subdir via university.mydomain.net/subdir
the address bar in the browser shows: university.mydomain.net/subdir no
matter if I access it via university.com/~username or university.mydomain.net
The problem with using permanent redirect with masking in GoDaddy, then if
I am in the dir university.mydomain.net/subdir and go to the subsubdir
university.mydomain.net/subdir/subsubdir then the browser still shows university.mydomain.net/subdir
And about CNAMEs or other kind of register in GoDaddy, I really have no idea
what to do (this is my first domain) and I don't even know if I have the rights
in my university server to make a difference.
I tried with a RewriteRule in a .htaccess file, but this always tries to
redirect (no matter what flag I use) to university.mydomain.com/subdir
while the only thing I want to do is that the address bar shows the direction
without following it.
I really don't even know if this is possible somehow. Sorry if it's an stupid
question, I'm very new with domains and those stuffs.
Under the CNAME you need to add you subdomain entry (in the Godaddy DNS management console)
university.mydomain.net points to #
and in your host the # might be pointing to the IP address where your university server is running.
Then in your apache vhost config, you would need to have rewrite rule like
<VirtualHost *:80>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^university.mydomain.net(:80)?$
RewriteRule ^/(.*) http://university.com/~username/$1 [P,QSA,L]
RewriteCond %{HTTP_HOST} ^university.com(:80)?$
RewriteRule ^/~username/(.*) http://university.mydomain.net/$1 [R=301,L]
</VirtualHost>

Apache mod_rewrite ANY subdomain(s) to root domain, unless existent as virtualdocumentroot

Say you've got an Apache2 virtual host setup, something like this:
/htdocs/parent1.com
/htdocs/sub1.parent1.com
/htdocs/sub2.parent1.com
/htdocs/parent2.net
/htdocs/parentn.org
Say you'd like to do this with VirtualDocumentRoot /htdocs/%0, so that you can add and remove virtual hosts without tinkering with your Apache configuration. That's important: please please no messing with htaccess files or httpd.conf every time a virtual host comes or goes - whether that host is a parent domain or not. In fact, say you're using AllowOverride None.
Anyway, the question is, how might you 301 redirect non-existent sub-domains to their corresponding parent domains without redirecting existent sub-domains?
I may have solved my own problem. However I would appreciate any feedback if somebody finds a problem with what I'm doing.
The following leaves alone any request to an arbitrary subdomain, as long as there exists a corresponding document root; but redirects any request to a subdomain which does not exist in the filesystem.
<IfModule rewrite_module>
RewriteEngine On
RewriteMap lowercase int:tolower
RewriteCond "/htdocs/${lowercase:%{HTTP_HOST}}" !-d
RewriteCond %{HTTP_HOST} "\.([^\.]+\.[^\.]+)$"
RewriteRule ^/(.*)$ "http://%1/$1" [R=301]
</IfModule>
Allows me to setup wildcard DNS and use name-based virtual hosting, without touching any configuration settings. Also, there's no htaccess involved. Just make your folder with any name like "/htdocs/[host.]domain.tld" and you're up and running. As far as I can tell, this doesn't really work with SSL/TLS (presumably something to do with %{HTTP_HOST}?), but secure sites are comparably few and better resolved by IP address than by hostname.

Redirect from one domain to another, without the user realizing it

I have a bunch of domains on on of my servers. I'd like to be able to redirect some domains to a different domain without the user knowing, so not a 301 redirect.
An example, redirect domain.com to masterdomain.com/sites/domain.com. So when visiting domain.com, the user would be displayed with the data on masterdomain.com/sites/domain.com.
Also masterdomain.com and domain.com are on the same server.
How could I do this using Apache? mod_rewritem mod_alias?
If both host names use the same document root, you can do this:
RewriteCond %{HTTP_HOST} =example.com
RewriteRule !^sites/example\.com(/|$) sites/example.com%{REQUEST_URI} [L]
This rule will prepend /sites/example.com if the requested URI path does not already starts with that.
GoDaddy calls this a domain Alais.
Here is a link where it is explained
http://webwizardworks.com/web-design/domain-alias.html