Welcome to WebmasterWorld Guest from 54.224.49.217

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

rewrite subdomain to folder

mod_rewrite subdomain to folder

     
8:33 pm on Jul 25, 2007 (gmt 0)

New User

10+ Year Member

joined:July 25, 2007
posts:4
votes: 0


Hello,

excuse my ignorance...i am experimenting for a while, but no success ;(

With CP i made some subdomains, eg "forums.mydomain.com"
for the main site www.mydomain.com

(The sub folders corresponding to the sub-domains are all in the root folder as generated via CP)

I need .htacces/mod_rewrite for:

To access forums.mydomain.com in the browser and it should redirect to "www.domain.com/forums"

and the name in the browser should stay forums.domain.com

(I thought this is brain-dead simple....but i really didnt get it to work...)

***

RELATED (the whole thing but backwards):

If i were to access www.domain.com/forums i need a rewrite so it goes to forums.domain.com

Thank you!

Georg

9:00 pm on July 25, 2007 (gmt 0)

Senior Member

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2002
posts:18903
votes: 0


Do you mean redirect to, or do you want a URL rewrite instead?

There is a world of difference in the end result.

.

I assume that you will want to redirect (www.)site.com/folder to subdomain.site.com to eliminate duplicate content first.

After that, I assume a rewrite from subdomain.site.com to site.com/folder/ will be required.

If that is the case, the question was asked several times in recent days, several recent threads have the answer you require.

9:27 pm on July 25, 2007 (gmt 0)

New User

10+ Year Member

joined:July 25, 2007
posts:4
votes: 0


what i have now is

RewriteEngine on
#
# Internally rewrite "translation-service" subdomain to /translation-services subdirectory
RewriteCond $1 !^translation-service/
RewriteCond %{HTTP_HOST} ^translation-service\.domain\.com
RewriteRule (.*) http://www.example.com/translation-services/$1 [L]
#

i assume this is a rewrite? It works except that the name in the browser changes. But it does its job.

Could you be so kind and explain what the difference would be in a redirect?

[edited by: jdMorgan at 10:36 pm (utc) on July 25, 2007]
[edit reason] example.com [/edit]

10:58 pm on July 25, 2007 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 0


> the name in the browser changes

It's an external redirect, because you included "http://example.com" in the substitution URL. Because you did not specify otherwise, it will be a 302-Found (Temporary) redirect.

Coded properly, it would be something like:


# Internally rewrite "translation-service" subdomain to /translation-services subdirectory
RewriteCond $1!^translation-service/
RewriteCond %{HTTP_HOST} ^translation-service\.example\.com
RewriteRule (.*) /translation-service/$1 [L]

For the sake of future growth, I strongly suggest that you name your subdirectory the same as your subdomain (as shown). This will allow expansion to an unlimited number of subdomains using only one rule in the future.

For example:


# Internally rewrite <subdomain>.example.com/<URLpath> to example.com/subs/<subdomain/<URLpath>
RewriteCond $1 !^subs/
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
RewriteRule (.*) /subs/%1/$1 [L]

-or-

# Internally rewrite <subdomain>.example.com/<URLpath> to example.com/<subdomain/<URLpath>
RewriteCond %{ENV:Rewrite-Done} !^Yes$
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
RewriteRule (.*) /%1/$1 [E=Rewrite-Done:Yes,L]

In both cases, a mechanism is required (and present in the code) to prevent an 'infinite' rewriting loop.

If you use the first method, then you can easily externally redirect direct client requests for the subdomain-subdirectories back to the subdomain to avoid duplicate content:


# Externally redirect client requests for example.com/subs/<subdomain>/<URLpath> to <subdomain>.example.com/<URLpath>
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /subs/
RewriteRule ^subs/([^/]+)/(.*)$ http://$1.example.com/$2 [R=301,L]

Jim

[edited by: jdMorgan at 10:02 pm (utc) on Mar. 13, 2009]