Forum Moderators: phranque

Message Too Old, No Replies

Redirect Root (Home Page) To Subdomain

         

MisterT

11:24 pm on Mar 22, 2012 (gmt 0)

10+ Year Member




I have a client who chose to build-out a new website on a subdomain.

For various reasons, they want to now redirect the root domain to the new website located on a subdomain. (rather than move the new website from the subdomain to the root)

Any suggestions as to the best way to do this?

g1smd

11:36 pm on Mar 22, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Use a RewriteRule to redirect those requests.

I'd redirect retaining requested page name for any and all pages.

MisterT

11:44 pm on Mar 22, 2012 (gmt 0)

10+ Year Member




thanks for your reply g1smd!

could you provide a sample of what the htaccess code might look like? i've tried a couple versions but can't get it working just right. :(

g1smd

1:20 am on Mar 23, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Let's see your code for discussion as per forum charter..

MisterT

4:08 pm on Mar 23, 2012 (gmt 0)

10+ Year Member



well, i tried some version of the canonical redirect we use, which is:

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

i just can't figure out how exactly to enter the code so this one goes smoothly, without screwing up other stuff. :(

g1smd

8:37 pm on Mar 23, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



This code will redirect requests to www.example.com.

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


Replace www.example.com with the place you actually want to redirect to.

MisterT

9:06 pm on Mar 23, 2012 (gmt 0)

10+ Year Member



cool thanks g1smd!

this code seems to work as far as redirecting the website to a subdomain, however it is redirecting ALL pages of the website to the new subdomain.

how would that code need to be changed so it redirects only the root (home page) to the new subdomain?

g1smd

9:14 pm on Mar 23, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



See the
(.*)
bit?

Change it to
^$
or to
!.
as both of those are equivalent.


I must say that it's a bit weird for the root of the main domain to redirect to a subdomain while the rest of the pages on the main domain continue to serve content.

MisterT

9:41 pm on Mar 23, 2012 (gmt 0)

10+ Year Member



cool, thank you very much, this works!

agreed, it is a weird way of doing things, however the client gets what the client wants i guess.

(the new website on the subdomain has a totally different architecture, which is partly why it's being done like this)

g1smd

10:17 pm on Mar 23, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If you want to redirect interior pages from the old site to the equivalent page on the new site but the path on the new site is different on that on the old site there's a simple way to that.

Rewrite (that's rewrite not redirect) requests for pages on the old site to a simple PHP script on the old site. Inside the script have an array which holds the old URLs and the new URL for each one. Detect what the originally requested URL was, look up the new URL in the array and send a HEADER directive with the 301 status and another with the redirected-to location. This is often a usable method for sites having up to several hundred pages.

MisterT

10:25 pm on Mar 23, 2012 (gmt 0)

10+ Year Member




thanks for the suggestion, i'll have to consider that

:)

MisterT

10:58 pm on Apr 27, 2012 (gmt 0)

10+ Year Member



Now I'm trying to do the same thing with a directory. I want to redirect a directory (and ALL pages within that directory) to a subdomain. I tried the below redirect but it doesn't work just right. I've tried a few variations, and none are working. Any suggestions? Anyone? Thanks!

RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www\.example\.com/directory/)?$
RewriteRule (^$) http://example.example.com/ [R=302,L]

g1smd

11:59 pm on Apr 27, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



HTTP_HOST can match only the hostname, not the path.

(^/) matches only the root, not any folders.

Do not use a 302 redirect.

Use example.com in code examples in this forum.

lucy24

6:28 am on Apr 28, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Three pieces to an URL. The domain ("host") can only be seen by a RewriteCond that looks for it. The query string, if any, can only be seen by ditto. The path-- the "meat" of the URL-- can be seen either by a Condition (REQUEST_URI and a few others others) or by the Rule.

Meta-Rule: Anything that can go into the Rule itself should go into the Rule. Conditions are only for the overflow. The ideal is a conditionless rule.

Here you want a Rule that contains the element

^(directory/.*) to capture the directory and anything inside it. Only the "Host" part stays in a Condition.

Would you ever get requests for non-pages-- images, for example-- in the "wrong" subdomain? If not, try to constrain the Pattern part of your Rule so it ends in (/|html) or (/|php) or whatever applies. That way the server never has to step back and evaluate the Condition in circumstances where it will always fail.

This Forum has, at a rough estimate, eight billion examples of the RegEx to capture a full URL that might contain any number of nested directories. Resist the temptation to use .* in mid-pattern. Yes, you may cut-and-paste the Regular Expression only. It's a pain to type, although it isn't long.

MisterT

7:41 pm on Apr 30, 2012 (gmt 0)

10+ Year Member



lucy24, thanks very much for the helpful reply!