Forum Moderators: phranque

Message Too Old, No Replies

Subdomain Redirect

         

leko3

7:13 pm on Jun 2, 2015 (gmt 0)

10+ Year Member



I'm a beginner with htaccess so I appreciate the help here. I have an addon domain on my main hosting account. I want to redirect the subdomain.

Through google I found these 2 different ways to redirect a subdomain.
What is the difference? Which is better?

# Subdomain Redirect - Method 1
RewriteCond %{HTTP_HOST} ^example2.example1.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.example2.example1.com$
RewriteRule ^(.*)$ "http\:\/\/www\.example2\.com\/$1" [R=301,L]


# Subdomain Redirect - Method 2
rewritecond %{HTTP_HOST} !^www\.example2\.com$ [NC]
rewriterule ^(.*)$ http://www.example2.com/$1 [R=301,L]

lucy24

8:41 pm on Jun 2, 2015 (gmt 0)

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



When you're redirecting a hostname-- any hostname, whether a subdomain or your primary-- express the condition as a negative: "if the requested hostname is anything other than this exact form (or exactly nothing, for certain elderly browsers or weird proxies) then redirect to my preferred form".

The first ruleset is badly written for several other reasons, so I advise you to cross that source off your Trusted Authorities list. The second ruleset is your vanilla domain-name canonicalization redirect, though again it's got some issues.

Before we can hammer out the optimal wording of your redirect, we need one rather crucial bit of information that's missing from your post: what requests do you want to redirect? Explain in English what the request says, and where you want it to go.

leko3

8:52 pm on Jun 2, 2015 (gmt 0)

10+ Year Member



Hmm very interesting thanks for your explanation lucy24!

we need one rather crucial bit of information that's missing from your post: what requests do you want to redirect?

My goal is to redirect
www.example2.example1.com
to
www.example2.com

But I don't know how to translate this goal into htaccess code.

leko3

8:54 pm on Jun 2, 2015 (gmt 0)

10+ Year Member



And I also want to redirect the non-www version as well

lucy24

12:07 am on Jun 3, 2015 (gmt 0)

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



Oh, wait, didn't someone else just ask this? Maybe it was you in a different thread. Something about add-on domains, right?

So anything for
subdomain.example.com
should go to
subdomain.com
?

Unless I've overlooked something, it's just two lines, rule with preceding condition:
RewriteCond %{HTTP_HOST} subdomain\.example\.com
RewriteRule (.*) http://www.subdomain.com/$1 [R=301,L]
Note that there are no anchors in the condition. That way, it covers with and without www, and also any appended port numbers (rare, but doesn't hurt to let the rule address those too).

Does this happen in the same htaccess that's used by example.com (the real thing)? If so, the domain-name canonicalization redirect for the rest of example.com will have to go after this rule.

Did subdomain.example.com ever exist as a visible subdomain in its own right? If so, you'll need to make sure every last internal link has been corrected. If not, the redirect is just for insurance; nobody should ever have any reason to suspect its existence.*


* Unless search engines are being downright malicious: Learn the IP, find out what host controls it, establish that their directory structure permits multiple routes to the same content, go out of your way to ask for URLs that nobody has ever mentioned anywhere.

Naah. They'd never do that.

leko3

1:00 am on Jun 3, 2015 (gmt 0)

10+ Year Member



Yeah that was me in a different thread... the long-winded one, I started this one because I had a more specific question
So anything for subdomain.example.com should go to subdomain.com ?

Yup exactly that! Basically these are 2 different websites sharing hosting and the side effect of cpanel addon domains is that it creates subdomains hence here I am and glad that people like you are here to help noobs like me! Hah. And no, the URL subdomain.example.com never ever existed or publicized.
Does this happen in the same htaccess that's used by example.com (the real thing)? If so, the domain-name canonicalization redirect for the rest of example.com will have to go after this rule.

Not sure I understand your question. Are you asking if this URL exists: example.subdomain.com ? No it doesn't seem to exist probably because example.com is the master hosting account so cpanel doesn't create a subdomain for it.

Can you check if I correctly pasted your subdomain redirect code in my htaccess file below? I pasted it BEFORE the # Domain-name canonicalization redirect

Another question. I put subdomain.com's folder outside of public_html. The htaccess file below is what I am putting in the folder of subdomain.com while example.com will be using the same htacces as below EXCEPT # redirect subdomain -- is this the correct approach?

Options -Indexes

<files wp-config.php>
Order allow,deny
Deny from all
</files>

<files error_log>
Order allow,deny
Deny from all
</files>

RewriteEngine On
RewriteBase /

# Redirect index.html and index.php
RewriteCond %{THE_REQUEST} [A-Z]{3,9}\ /(([^/]+/)*)index\.(html|php)
RewriteRule index\.(html|php) http://www.subdomain.com/%1 [R=301,L]

# Force Trailing Slash
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://www.subdomain.com/$1/ [R=301,L]

# Redirect Subdomain
RewriteCond %{HTTP_HOST} subdomain\.example\.com
RewriteRule (.*) http://www.subdomain.com/$1 [R=301,L]

# Domain-name canonicalization redirect
RewriteCond %{HTTP_HOST} !^(www\.subdomain\.com)?$ [NC]
RewriteRule (.*) http://www.subdomain.com/$1 [R=301,L]

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

lucy24

4:14 am on Jun 3, 2015 (gmt 0)

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



Are you asking if this URL exists: example.subdomain.com?

No, I never thought that was a possibility. Subdomains only operate in one direction.

is this the correct approach?

Do everything you can to prevent requests for one domain passing through the htaccess file of a different domain. Or rather: do everything you can to prevent requests for one domain passing through RewriteRules meant for a different domain. Thanks to its wonky inheritance rules, you never, ever want the same request passing through more than iteration of mod_rewrite. (There are exceptions. Do as I say, not as I do.) Rules that are the same for all domains, such as expiration headers and ErrorDocument directives, can go in an htaccess file that is shared by all (sub)domains. Rules that are specific to one domain should, if possible, go in an htaccess that is only seen by that domain.

How you do this depends on your server's overall directory structure.

Version A:
/example.com/
  .htaccess
  index.html (i.e. example.com/index.html)
  /directory1 (files for example.com/directory1)
  /directory2 (files for example.com/directory2)
  /othersite (files for othersite.com, with further subdirectories, optionally including a further htaccess)
  /thirdsite (files for thirdsite.com, ditto)

Version B:
/yourname/
  .htaccess (optional in this location)
  /example.com (all files for example.com, optionally including its own htaccess)
  /othersite (all files for othersite.com, ditto)
  /thirdsite (all files for thirdsite.com, ditto)

See the difference? In the first version-- call it the "primary/addon" directory structure-- example.com MUST share an htaccess with its addon domains, while the addons can have further htaccess files of their own. In the second version-- call it the "userspace" structure-- all domains are exactly parallel, and there may or may not be an htaccess shared by all domains. It's important to remember that htaccess is based entirely on physical directory structure, which may or may not have anything to do with URL. (Years ago, one directory on my personal site was aliased to my son's userspace: my URL, his physical directory. It took me ages to understand why access-control rules had no effect on this directory.)

If you're using a CMS such as WordPress, pay very very close attention to exactly where the htaccess files are located and watch out for potential conflicts.

Edit:
####. I added a bunch of stuff, hit Post, and was informed of an error with style tags-- with no way to go back and fix it. Well, ### then. I hope it wasn't anything important.

You do seem to have at least three occurrences of the "RewriteBase" line, which is at least three more than you need. If one of them is inside the WordPress block, leave it alone. You can delete all the others.

leko3

5:38 am on Jun 3, 2015 (gmt 0)

10+ Year Member



Hmm I think I am using Version B structure.

Crap, I realized I was unclear in my last reply. Actually I am NOT reusing the same exact htaccess file for both example.com and subdomain.com - what I meant to say is, I am using the same htaccess file except I swap the domains.

For instance (without having to post the entire htaccess file) partial example of the htaccess file for example.com will be

# Domain-name canonicalization redirect
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$ [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress


While partial example of the htaccess file for subdomain.com will be

# Domain-name canonicalization redirect
RewriteCond %{HTTP_HOST} !^(www\.subdomain\.com)?$ [NC]
RewriteRule (.*) http://www.subdomain.com/$1 [R=301,L]

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

Notice that both htaccess files has the Wordpress htaccess code - is this correct?

And notice I swapped the domains for each htaccess file. In other words, each htaccess file is tailored for each domain.

p.s. In my htaccess file in my last reply, is it correct to paste your subdomain redirect code BEFORE the # Domain-name canonicalization redirect ?

Thanks for the tip about the Rewrite Base. I will delete the 2 repeats.

lucy24

4:53 pm on Jun 3, 2015 (gmt 0)

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



both htaccess files has the Wordpress htaccess code - is this correct?
If each place has its own separate WP installations, then that's how it has to be.

subdomain redirect code BEFORE the # Domain-name canonicalization redirect
Yup, because there are two steps: FIRST redirect requests for "subdomain.example.com". THEN redirect the leftovers within "example.com". These two lines only appear in the htaccess for example.com.

Double-checking here: example.com and subdomain.com are on separate (physical) paths, so neither one will ever see the other one's htaccess? That's a big help.