Change domain of URL but keep string

It’s been a while since I have touched anything mod_rewrite related. Does anyone have a solid method for redirecting domain names but keeping the rest of the URL?

I want to change this:
www.domain1.com/something/here

to:
www.domain2.com/directory/something/here

Doesn’t surprise me, as $2 is not defined. It should be


RewriteCond %{HTTP_HOST} ^([a-z.]+\\.)?domain1.com$ [NC]
RewriteRule ^(.*)$ http://www.domain2.com/newfolder/$1 [L]

%{HTTP_HOST} only contains the domain, not any directory within that domain. Think about it.

Gib,

Have you read the sticky thread? The tutorial linked in my signature? :nono:

Have a look then come back if you didn’t find EXACTLY what you’re asking for.

Regards,

DK

I actually did read through. Granted I didn’t take the 1-2 hours to read the whole thing but I went through the examples and didn’t see anything that either related or I could manipulate for my scenario.

Most the of the examples are focused on adding or removing extensions or rewriting query strings (?foo=bar).

From reading through your postings and google searches I get the below…

RewriteCond %{HTTP_HOST} ^([a-z.]+\.)?domain1.com$ [NC]
RewriteRule ^(.*)$ http://www.domain2.com/newfolder/$1/$2 [L]

However this leaves a trailing slash (/) and generates a 404 page. One suggestion I showed that the code ^(.+)/$ was supposed to eliminate the trailing slash but that didn’t work for me.

I also am able to be as straight forward as…

RewriteCond %{HTTP_HOST} ^([a-z.]+\.)?domain1.com/child/page$ [NC]
RewriteRule ^(.*)$ http://www.domain2.com/newfolder/child/page [L]

But this never runs. it just bypasses and goes to my catch all that follows this rule.

So I got it working with this:

RewriteCond %{HTTP_HOST} ^([a-z.]+\.)?domain1.com$
RewriteRule ^(.*)$ http://www.domain1.com/directory/$1/$2 [L]

The problem now is there is always a trailing slash on the redirect.
www.domain2.com/directory/something/here/

I need it to be
www.domain2.com/directory/something/here

I’ll continue searching for a solution and post here if I find it. If anyone knows how to remove the trailing slash please do share.

That is what I was missing. I didn’t understand was $1 was. I didn’t realize it equals the rest of the URL. Thank you very much for the help.

Is there a way to put an if statement around $1?
Something like.
http://www.domain2.com/newfolder [IF $1] /$1[END IF]

So if $ is populated it goes to
http://www.domain2.com/newfolder/$1

If it’s not then
http://www.domain2.com/newfolder

I’m using this…

RewriteCond %{HTTP_HOST} ^([a-z.]+\\.)?domain1.com$ [NC]
RewriteRule ^(.*)$ http://www.domain2.com/newfolder/$1 [L]

Gib,

:rofl: Oh, well, hours of compiling member questions and replies gone to waste!

RewriteCond %{HTTP_HOST} ^([a-z.]+\\.)?domain1.com$ [NC]
RewriteRule ^(.*)$ http://www.domain2.com/newfolder/$1/$2 [L]

I see the problem! Where do you create $2?

%1 is created by the RewriteCond but it also includes the dot character (twice!) so that’s not a good thing. If you want JUST the subdomain (and NOT want it to include www), try

RewriteCond %{HTTP_HOST} ^([a-z]+)\\.domain1\\.com [NC]
RewriteCond %1 !www [NC]

Then you can capture all the garbage you want with the RewriteRule and redirect it to {wherever}/%1$1 (because %1 was captured by the RewriteCond and $1 will contain the / from the original {REQUEST_URI}. Try it!

Regards,

DK