Link to home
Create AccountLog in
Avatar of dealclickcouk
dealclickcouk

asked on

Managing subdomain redirects

We manage individual servers in our cluster by giving each a subdomain which stats its role;

ie admin.domain.com, www.domain.com, bak.domain.com

To enable hotswaps and clustering etc all servers are configured the same, but for example the admin. server will also have additional backend administation software...

The problem is that we access the admin back end by http://admin.domain.com/admin/, but for some reason we now have admin.domain.com and www.domain.com results in google serps.

I would like away to redirect admin.domain.com visitors to www. but still allow access to admin.domain.com/admin/.

I thought a 301 redirect in .htaccess would do the trick (ah yes the servers are apache!), but I'm not sure of the syntax to make sure it doesn't redirect visitors to the /admin/ directory.

Thanks
Avatar of McKirk
McKirk
Flag of Germany image

I guess there is a easier way.

Your apache config has the entry "documentRoot". This is, where your "website" is saved. In that directory is the subdirectory "admin".
You just could put a simple PHP file in the documentRoot with this content:

<?php
header("Location: http://www.domain.com");
?>

That means, that every request to admin.domain.com will be redirected to www.domain.com, but request to any subdirectory (like admin.domain.com/admin) will not be affected.

Of course you will have to have PHP working.
Avatar of dealclickcouk
dealclickcouk

ASKER

Ah ok, but the domain use apache rewrite, so how would that be effected....

ie

admin.domain.com/products/12345/mp3player.php
should go to:
www.domain.com/products/12345/mp3player.php

however:

admin.domain.com/admin/
should not redirect anywhere...

I think your redirect would just work for a single domain redirect, rather than retain mod_rewrite path?

Thx
Okay, you're right. Sorry.

Try this:

RewriteCond %{HTTP_HOST}   !^www\..* [NC]
RewriteCond %{HTTP_HOST}   !^.*\admin [NC]
RewriteRule ^(.*)          http://www.%{HTTP_HOST}/$1 [R=301]

This is redirecting all request without www at the beginning and without /admin as first directory to www.domain&
ok tried that doesn't seem to work

admin.domain.com still displays the page on the admin server

and http://admin.domain.com/dept/appliances.php

gives a file not found error (because I removed the redirect stuff in the .htaccess file and replaced with your code above)

Check, if your apache's conf does allow configuration within htaccess files.

yes, because it was already using .htaccess for redicts and cache settings.
OK two things have got us closer...

First I added:  RewriteEngine On, but then still got a page not found error...

Second, viewed the url with SeaMonkey which gives a much nicer error:

www.admin.domain.com could not be found. Please check the name and try again.

note the admin. had not been removed, just www. added, so if the .htaccess can be tweaked to remove this then we have a winner
Okay, we'll try one after another.

First the redirection of ALL from admin.* to www.*

RewriteEngine on
RewriteCond %{HTTP_HOST} !^admin.domain.com$
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]  

Please confirm, that this do redirect ALL trafic from admin to www (after replacing domain.com with your domain :-)).
nope that doesn't work, now I see the pages on the admin server
Sorry, my mistake:

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.domain.com$
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]  

It should redirect all pages WITHOUT www at the beginning, not WITHOUT admin (like it my last post).
ok great that works for the normal www. redirect, but not for the /admin/ ie that is still redirected.
So I guess another condition is required?
Okay, good so far.

Nor we add the second condition:

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.domain.com$
RewriteCond %{HTTP_HOST}   !^.*/admin [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]  

I meant NOW not nor and please pay attantion to the slash (not a backslash!).
nope, that still redirects the /admin/... stuff to www.
ASKER CERTIFIED SOLUTION
Avatar of McKirk
McKirk
Flag of Germany image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
perfect, many thx
thx for all your help and hand holding, cant have been easy ;)