views:

87

answers:

3

Hello,

I really tried tons of methods but i'm not successful. I want a .Htaccess code to do the following :

I want to rename this : http://www.mydomain.com/media --> http://media.mydomain.com

So, By example instead of calling this : http://www.mydomain.com/media/XXX/picture.jpg i will call : http://media.mydomain.com/XXX/picture.jpg

Thank you

A: 

Make sure rewrite_module is loaded, something like;

LoadModule rewrite_module /usr/lib/apache2/modules/mod_rewrite.so

Then add the following (to your .htaccess):

RewriteEngine on
RewriteCond %{HTTP_HOST} www.mydomain.com
RewriteRule ^/([^/]*)(.*) http://$1.mydomain.com$2 [L,R]

The Cond will only match www.mydomain.com. The Rule then will split the URL into 2 using the first '/' (which will be included in $2), rewrite and redirect.

thanks niry, but that's not workin :(
David
@niry: I believe you've answered the reverse of what the OP wants (but I can't tell for sure), e.g. the user enters `sub.domain/blah` and the server serves `domain/sub/blah`. (BTW, backticks work for code as in your last comment.)
Roger Pate
A: 

It's gonna be like this:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(?!www\.)([^.]+)\.mydomain\.com$
RewriteRule (.*) http://www.mydomain.com%1$1 [L,R=301]
TonyCool
A: 

If you want the opposite (see Roger's comment) and without redirecting the user

RewriteEngine on
RewriteCond   %{HTTP_HOST}                 !^www\.mydomain\.com$
RewriteRule   ^(.+)                        %{HTTP_HOST}$1          [C]
RewriteRule   ^([^.]+)\.mydomain\.com(.*)  http://www.mydomain.com/$1$2 [L]

Also, see here: http://httpd.apache.org/docs/1.3/misc/rewriteguide.html