Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Redirect Directory on One Domain to Subdomain on Different Domain

Status
Not open for further replies.

MrTBC

Technical User
Nov 19, 2003
610
US
I need to redirect all URLS relating to a subdirectory on one domain to a subdomain on another domain, e.g.


Should be redirected to:


This needs to work for:

etc.

Please note that the directory needs to be removed from the first domain and the subdomain needs to be added to the second domain.

Can this be achieved using .htaccess please?

Thanks very much for your help.
 
Thanks, but you have included the specific URL (i.e. /anythingatall) and I need to redirect all URLs please.
 
Thanks very much elgrandeperro and smah. Smah - I will give this a try.
 
Smah - thanks for your help and apologies for not replying sooner. It looks as if your code redirects ALL of the URLs on the first domain to the subdomain on the second domain.

What I'd actually like to do is only redirect URLs within one directory on the first domain, so:


Should redirect to:


but:


shouldn't redirect.

Thanks very much.
 
Where did you put the code? If you put it in the config OR on the root, that is what is going to happen. I assumed you would put it in .htaccess of 'directory'. If it it is the config, it will need a tweak.
 
Sorry elgrandeperro. I'm using a CMS so my "directories" aren't actually directories, they're just URLs created on the fly.

I guess by trying to simplify the question I made it more complicated.
 
Looks like you have a lot of variables at work here. A few questions.

Are domainone and subdomain hosted on the same Apache instance?
If so, are you using virtual hosts?
If so, do you have a separate virtual host for each for both domainone and subdomain?

I see that you have two examples /something and /somethingelse. Will you have more than two folders that you need to match? If so, are you planning to create a rule for each folder? The reason I'm asking is there are both hard coded and possible dynamic solutions to the redirect you are trying to implement.

Rob Jordan
 
Another thought. A redirect will show the subdomain the client's browser. The proxy option may be better suited if you want to mask the second domain from the end user. So in the example below, the user would see in their browser, however they would get the contents of the URL
Here's the same rule as a proxy.

# verify the rewrite engine is on
RewriteEngine On
# this is optional and limits the rule to the domain below
RewriteCond %{HTTP_HOST} domainone.com
# the contents inside the parens are sent to $1
RewriteRule ^/directory/(.*) [P]

Rob


Rob Jordan
 
Thanks Robert, I've tried your first solution and it won't work I'm afraid - it still goes to the URL on domainone.com

To answer your questions:
1. The two domains are hosted completely separately.
2. All URLS are in the format:


With no additional "/" symbols after directory/

3. There is no need for a Proxy thanks.
 
This should work then. Since the configs are separate, you shouldn't need the RewriteCond. I'm assuming you are placing this in your apache.conf file.

# you may also want to debug your rules with a log
RewriteLog /your/log/location/rewrite_log
RewriteLogLevel 9

# the contents inside the parens are sent to $1
RewriteRule ^/directory/(.*) [R=301,L]

Rob

Rob Jordan
 
Apache treats anything to the right of the ? in the request as query. The ? and query will be passed along with the redirect unless you explicitly tell apache not to pass along the query by placing a ? at the end of the target redirect.

example of the same rewrite rule, however the ? at the end will truncate the ? and anything after that on the redirect.

# the contents inside the parens are sent to $1
RewriteRule ^/directory/(.*) ? [R=301,L]

Rob

Rob Jordan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top