Forum Moderators: phranque

Message Too Old, No Replies

Mod_rewrite EVERYthing to second domain?

Subdomains, and query strings, too?

         

Alfie

12:57 am on Dec 9, 2003 (gmt 0)

10+ Year Member



Hello there.

I feel silly as if I'm having problems putting two and two together from all the mod_rewrite documentation and examples out there, but I was wondering how to get this done.

I have two domains, a short one I moved off of (and now have a whopping 2MB of space on thanks to a donation from a friend), and a long one I use now. Unfortunately, this second one is much longer and apparently one of the HARDEST words to type on the Internet (I'd say why, but it'd sidetrack the point). So basically, I want to make it so that when anybody uses the old domain (which is easy to type and remember), it will simply redirect the request to the new one (the latter part of which is left-pinky intensive).

I use this .htaccess currently:


Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP-HOST}!^www\.as\.tld
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)$ http://www.asdfasdfasdfasdf.tld/$1?%1 [R=permanent,L]

Maybe I mistyped something there, but it works like a champ in practice. However, I would like to make it so that all the subdomains that get put before the shorter name ALSO get through, so it can effectively act as a shortcut name for the longer domain. I simply haven't learned all this regex and backreference stuff yet, but it's kinda important I get this done soon.

(If it's OK to give a real-life example of this in action, sf.net/sourceforge.net is set up this way)

Thanks,
BRPXQZME

claus

1:03 am on Dec 9, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



- take a look at post #10 in this thread for the subdomain issue, it's not a piece of cake but not rocket science either: Redirecting of Subdomain [webmasterworld.com]

- you'll of course have to do minor edits to get it working. I think you'll get by by changing it to something like this (it's just a quick edit):

-------------------------------------------

RewriteEngine on 
RewriteCond %{HTTP_HOST} ^(www\.)?[^.]+\.as\.tld
RewriteRule ^(.+) %{HTTP_HOST}$1 [C]
RewriteRule ^(www\.)?([^.]+)\.as\.tld(.*) http://$1.asdfsdsds.tld/$2 [R=301,L]

-------------------------------------------

This might even do the whole thing by itself - at least as far as subdomains are concerned. Give it a try and let's hear if it works for you. Oh, this might need to be changed:

From [^.] to [^\.] but i'm not sure it really needs to.

/claus

Alfie

1:34 am on Dec 9, 2003 (gmt 0)

10+ Year Member



Hm... sorry, Claus, it doesn't work. What happens is that as.tld will GO to as.tld, www.as.tld will go to [.asdfasdfasdfasdf.tld,...] and foo.as.tld will end up at foo.as.tld ...

Just to make it really, really clear what should happen:

[[(1).]as.tld[...] -->
[[(1).]asdfasdfasdfasdf.tld[...]

Of course it isn't rocket science. It's quantum physics as far as I know :P

jdMorgan

1:48 am on Dec 9, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



http://[(1).]as.tld[/(2)][?(3)] --> http://[(1).]asdfasdfasdfasdf.tld[/(2)][?(3)]

How about this:


RewriteCond %{HTTP_HOST} ^(.*)?as\.tld
RewriteRule (.*) http://%1asdfasdfasdfasdf.tld/$1 [R=301,L]

The query string should be passed automatically without any further work. If not, re-post.

Jim

Alfie

2:02 am on Dec 9, 2003 (gmt 0)

10+ Year Member



That works identically to the code I was using, despite the changes, aside from the fact that omitting www will omit www in the redirected address as well. Looks like it should work, too.

claus

2:08 am on Dec 9, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry about that. I did say that you'd have to do minor edits as i wasn't quite sure it would work straight away, it was just a quick guess ;)

Let's try this one, i hope it's a little closer (i'll skip the options symlinks and the rewrite engine, they should be there as well):

-------------------------------------------

RewriteCond %{HTTP_HOST} as\.tld$
RewriteRule ^(.+) %{HTTP_HOST}$1 [C]
RewriteRule ^(.*)?as\.tld(.*) http://$1asdfsdsds.tld/$2 [R=301,L]

-------------------------------------------

The condition is matched as long as the domain ends in "as.tld"

Rule 1: Anything after the host name is appended to the host name and the whole thing is chained to rule 2

Rule2: Anything between the start of the string and "as" is put before the long domain and anything after "tld" is put after "tld" on the long domain.

At least that's what it's supposed to do - i hope it does it as well this time. I'm off for now, but i'll pop in tomorrow, it's 3AM here.

Added: Just saw the shorter version in post #4. If that one works, use it in stead - unnecessary complexity is not good.

/claus


BTW: Welcome to WebmasterWorld Alfie :)

jdMorgan

2:28 am on Dec 9, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Alfie,

One of the problems here is that it's not completely clear what your possible 'short' domain names look like, and what the 'long' output for each of those should be. It might be helpful if you posted a list like

www.as.tld -> www.asdf.tld
as.tld -> www.asdf.tld
test.as.tld -> www.asdf.tld/test
etc.

showing as many unique variations as possible.

Jim

Alfie

10:35 pm on Dec 9, 2003 (gmt 0)

10+ Year Member



Muh. Tried just about everything, and I fail to see why those last two shouldn't work. I think it probably has something to do with the way the server is set up.

I guess it's something I can live without for a while. Maybe come up with some convoluted solution. Thanks for the help anyway.