views:

525

answers:

3

I have a sub domain that I want to redirect to a different location using mod_rewrite. For example:

subdomain.example.com -> www.example.com/subdomain

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

BTW subdomain.example.com has a CNAME record pointing to example.com.

Edit

Another example, just to clarify. It is very simple: If http://x.abc.com is entered in to the browser, Apache returns the contents of http://www.abc.com/x.

+3  A: 

If both domains share the same directory, you could do this to rewrite the requests internal:

RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$
RewriteRule !^subdomain/ subdomain%{REQUEST_URI}


After the question has been clarified: This one works for me:

RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$
RewriteCond %1.%{THE_REQUEST} ^([^.]+)\.[A-Z]+\ (/[^\ ]*)
RewriteCond %{DOCUMENT_ROOT}$1 !-d
RewriteRule ^([^/]+)? %1%2

But you better go with what Brandon said.

Gumbo
I'm not sure I follow your example above, but what I want to do is very simple: If http://x.abc.com is entered in to the browser, Apache returns the contents of http://www.abc.com/x. Thanks!
Abdullah Jibaly
+1  A: 

In addition to what Gumbo stated above, you can use Apache's VirtualDocumentRoot statement to dynamically map URL to a dynamic location on disk. It allows you to use parts of the URL to build a path on disk. Check out this link for more information:

http://httpd.apache.org/docs/2.2/mod/mod_vhost_alias.html

Example:
URL = http://sub.example.com/dir/page.html
Server Path = /var/www/site.com/htdocs/sub/dir/page.html
VirtualDocumentRoot = /var/www/%-2.0.%-1.0/htdocs/%-3/

Brandon
A: 
RewriteCond %{HTTP_HOST} ^([^.]+)\.abc\.com$
RewriteRule ^(.*)$       http://www.abc.com/%1$1   [R=301]
Sean Bright