Forum Moderators: phranque

Message Too Old, No Replies

redirect from directory to subdomain

using mod_rewrite

         

badtigger

6:25 pm on May 27, 2005 (gmt 0)

10+ Year Member



If I want to redirect users from a directory to a new subdomain, would something like this be best?

RewriteEngine on
RewriteCond %{HTTP_HOST}!^(www\.)?mysite\.com\old_directory [NC]
RewriteCond %{HTTP_HOST} ^(.+)\.mysite\.com\old_directory [NC]
RewriteRule .* [subdomain.mysite.com...] [R=301,L]

badtigger

8:09 pm on May 27, 2005 (gmt 0)

10+ Year Member



After reading the rewrite docs from Apache, and the following threads:
[webmasterworld.com...]
[webmasterworld.com...]
and especially this:
[webmasterworld.com...]

I came up with code that should do what I want it to, but it still does nothing. The following code should redirect:
http://example.com/directory
to
http://subdomain.example.com
(and show it as such in the address bar)

what I figured out was:

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com\.directory
RewriteRule (.*) http://subdomain.example.com/$1 [R=301,L]

I tried this in the root directory, as well as the example.com/directory. Neither work. I seriously dont get it -- what could I be doing wrong?

[edited by: jdMorgan at 9:31 pm (utc) on May 27, 2005]
[edit reason] Example.com [/edit]

jdMorgan

9:28 pm on May 27, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You've tried to include part of the URL in the hostname. HTTP_HOST will include only the hostname and port number, so your RewriteCond will never match. Try:

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule ^directory(.*) http://subdomain.example.com$1 [R=301,L]

Jim

badtigger

11:18 pm on May 27, 2005 (gmt 0)

10+ Year Member



thanks jd!

What did this for me is first putting a redirect in the old subdirectory, like this:

redirect /dir/index.php [subdomain.example.com...]

then putting the mod_rewrite code in the newly created subdomain to rewrite the URL correctly:

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule ^directory(.*) [subdomain.example.com$1...] [R=301,L]

so then, summarily, the simplest approach to direct visitors from one directory to a new subdomain and have the URL of that subdomain seamlessly rewritten in the address bar is:

1) put in redirect in the old directory's htaccess
[example.com/dir]
2) put in rewrite code in the new subdomain's htaccess [http://subdomain.example.com]

fauzzo

9:25 am on May 28, 2005 (gmt 0)

10+ Year Member



Hi,

I am trying to have it work too,

with the sample code here around I am always getting errors about not found pages.

What I ask is: should DNS setting for such domain rewriting (directory to subdomain) be changed?

Should I set the DNS so that *.domainname.com will work and point to domainname.com?

Or is apache configuration the only thing to be set?

Actually my domain DNS is set to work with [domain.com...] and [domain.com...]

What I would like to get is:

[domain.com...]

to:

[user.domain.com...]

thanks

jdMorgan

6:17 pm on May 28, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> What I ask is: should DNS setting for such domain rewriting (directory to subdomain) be changed?

Yes, you will need to set up "wild card" DNS.

> Should I set the DNS so that *.domainname.com will work and point to domainname.com?

No, set it up so that "*.domainname.com." will point to the same IP address as domainname.com.

After the new DNS settings propagate (4 to 48 hours), you should be able to access "<anything>.domainname.com" and then you can work on rewriting subdomain requests to subdirectories.

Jim

fauzzo

12:20 am on May 29, 2005 (gmt 0)

10+ Year Member



thanks for the help!

About DNS, I am going to use a virtual host, so the same IP is used for many domains names, and the one I would like to rewrite is not the one who is showed when you use IP.

Are these DNS setting correct?

domain.com. IN A 1.1.1.1 ; Nohost
www IN A 1.1.1.1
* IN A 1.1.1.1 ; catchall

(1.1.1.1 is the shared IP used for this virtualhost)

Thanks a lot!