views:

1052

answers:

2

I have a file that I want to redirect to a subdomain using mod_rewrite. For example, I would like to redirect the request for http://www.mydomain.com/xyz to the subdomain xyz.example.com

I don't want to send a redirect to the browser though (so it doesn't know the page is different).

BTW. xyz is a CNAME record, pointing to www.otherdomain.com.

Another example, just to clarify. If http://www.mydomain.com/xyz/something is entered into the browser, I want Apache to return the contents of http://xyz.mydomain.com/something

+2  A: 

I THINK this will do what you want it to:

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(.*?)/(.*?)$ http://$1.mydomain.com/$2

As this stands it will redirect EVERY request that goes to a subfolder so if you links for images or css that are in a sub folder it will also rewrite those, if you want to list only certain things you could use this

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(blog|store|other)/(.*?)$ http://$1.mydomain.com/$2

This will only redirect mydomain.com/blog/ mydomain.com/store/ and mydomain.com/other/

Unkwntech
Do you also need the proxy [P] flag at the end of the RewriteRule?
jcrossley3
Don't you need to start it with a ^/ ?
gahooa
@jcrossley3 - I'm not sure @gahooa - a ^ yes but not a /
Unkwntech
Hi Unkwntech, thanks for help."something" is a long PHP query.I used the first code you proposed,and it's working half way.It loads the xyz subdomain content properly, but in the address bar it shows:xyz.mydomain.com/something (instead of www.mydomain.com/xyz/something) to be continued
and if i put in the same address bar again:www.mydomain.com/xyz/something ( and hit enter)then it loads again properly, and in the address bar it stays (correctly):www.mydomain.com/xyz/somethingAfter i press refresh button it loads ok, but the address bar changing to xyz.mydomain.com/something
Remarks.1. www.otherdomain.com is on different server2. [P] at the end doesn't work (either distorted display or proxy error)3. ^/ at the beginning doesn't work
Message to Gumbo.Thanks for your reply. Tried your second code (with Options +FollowSymLinks) and it gives an error page not found.The code is for .htaccess file in root directory.
+1  A: 

If both domains are on the same machine and accessible over the filesystem, you should go this way. For example:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$
RewriteRule ^([^/]+)/(.*) /absolute/path/to/subdomains/$1/$2

If this is not the case, you’ll need a proxy:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$
RewriteRule ^([^/]+)/(.*) http://$1.example.com/$2 [P]

These examples are inteded for a .htaccess file. If you want to use them in the httpd.conf, prefix the RewriteRule patterns with a /.

Gumbo
Always fix the domain name when applying that. This is shown in the example but I wanted to point out that readers should make sure NOT to provide access to any other domains through the proxy; this has been done by excluding / as a character in the regexp and giving proxy access only to subdomains.
Energiequant