Forum Moderators: phranque

Message Too Old, No Replies

.htaccess redirect from sub.domain1.com to sub.domain2.com

         

echoniner

9:11 am on Oct 22, 2010 (gmt 0)

10+ Year Member



I am really struggling with this problem, and have tried every thing I can think of in my limited .htaccess knowledge to make this work:

I want to redirect every call to any page from one subdomain.domain.com to another, with variable parameters.

For instance, I want

http://subdomain.olddomain.com/index.php?many_variables_here


to permanently redirect to (with the addressbar in the browser reflecting the new)

http://subdomain.NEWdomain.com/index.php?many_variables_here


I'm redirecting one subdomain only to the same subdomain name at another domain (permanently...want all expired links to still work). That is, www.olddomain.com still exists and is not to redirected, just subdomain.olddomain.com is to redirect.

For what it's worth: olddomain is an add-on domain of newdomain.

Thanks!

jdMorgan

12:27 pm on Oct 22, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



As a basis for further discussion:

What have you tried so far?

Where (in what .htaccess file location) did you put the code?

Jim

echoniner

6:04 pm on Oct 22, 2010 (gmt 0)

10+ Year Member



i discovered by trial and error using other .htaccess rules that I only need a .htaccess file at the newdomain.com (again, newdomain.com is the top-level on the server, the "primary domain" for the account, while olddomain is an add-on, and so resides in a folder one level down from public_html.)

As for what I've tried, that may be irrelevent because I only know enough to fumble my way through. For instance, I tried this:


RewriteCond %{QUERY_STRING} sub.olddomain
RewriteRule ^sub.olddomain sub.newdomain [R=301,L]


I also tried this:


RewriteCond %{HTTP_HOST} ^(sub\.)?olddomain\.com$ [NC]
RewriteRule ^(.*)$ http://sub.newdomain.com/$1 [R=301,L]


In both cases I preceed it with a Options +FollowSymLinks line. I also temporarily removed my real .htaccess with all of its code to have just these attempts above, to ensure I didn't somehow have code that was interferring with the above.

jdMorgan

8:04 pm on Oct 22, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> I only need a .htaccess file at the newdomain.com

This will not be true if olddomain.com was defined as an "add-on domain" using a typical shared-hosting "control panel" method.

You will need to put your new rule into the folder where olddomain's files are (or were) stored.

To specifically redirect only the single "subdomain1" subdomain of oldomain.com, use:

RewriteCond %{HTTP_HOST} ^subdomain1\.olddomain\.com [NC]
RewriteRule ^(.*)$ http://sub.newdomain.com/$1 [R=301,L]

Query strings will be passed through unchanged by default.

Jim

echoniner

7:38 am on Oct 23, 2010 (gmt 0)

10+ Year Member



Thanks. It seems to be redirecting, except the above code redirects to:

http://sub.newdomain.com/sub/index.php?variables_here


Now, of course the subdomain is technically a subfolder of the domain, as you no doubt well know, but now it appears twice in the URL. any ideas?

Tech notes:

Yes, the olddomain.com is defined as an "add-on" domain using cpanel.

Also, I have set up (mostly as a temporary fix) sub.olddomain.com as a subdomain, pointing to the very same folder on the host account that sub.newdomain.com does, so that, until I get this redirection working, any calls to the old link will still come up with a page and not a dead link.

thank you so very much for you expertise and time, jdMorgan!

[edited by: echoniner at 7:42 am (utc) on Oct 23, 2010]

jdMorgan

5:02 pm on Oct 23, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Before acting, stop and re-post your "error report" above.

Thanks. It seems to be redirecting, except the above code redirects to:

http://sub.newdomain.com/sub/index.php?variables_here


Which may be perfectly-good as far as we can tell...

For what requested URL does it redirect to http://sub.newdomain.com/sub/index.php?variables_here ?

I will guess that you've got your rules out of order [webmasterworld.com], and that an internal rewrite in this file or in a server config file is being invoked first, with the internally-rewritten filepath being exposed as a URL by this subsequently-invoked external redirect. But if you don't describe your test case, then any theory posted here is just a guess, and likely a waste of time.

Jim

echoniner

7:29 pm on Oct 24, 2010 (gmt 0)

10+ Year Member



Thanks again for the replies.

First, in cpanel, I have

sub.domainnew.com setup to the folder public_html/sub


I also have

sub.domainold.com setup to same folder public_html/sub


(no cpanel redirection enabled on the latter, as it does not work except as the simplest link: sub.olddomain.com without any index.php or variables in the URL.)

I have found that I require to have the old sub.domainold to be setup in cpanel as a sub-domain or I otherwise get a 404 not found error for links to the old site, no matter what I do with the .htaccess it seems.

I have also, via trial and error, found that placing the .htaccess in the folder public_html/olddomain does nothing on my host. (Remember, olddomain.com is a cpanel "add-on" domain on the primary account, which is setup under newdomain.com.) Thus, I only place the .htaccess under public_html.

So, I put the following into a blank .htaccess and put it at the public_html level alone:


Options -Indexes

#Options +FollowSymLinks

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


I put nothing else in my .htaccess. In the above, the result, when some link is followed to the old site, such as

http://sub.olddomain.com/index.php?variables


it forwards to

http://sub.newdomain.com/sub/index.php?variables


producing a HTTP 404 Not Found error.

Note: in all case, the subdomain and the folder it points to under public_html are identical.

Thanks!

jdMorgan

9:52 pm on Oct 24, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Somewhere you have an internal rewrite that changes an incoming URL request for [sub.olddomain.com...] to the filepath /sub/index.php?variables

This rewrite is invoked before your own code (what you just posted above).

This may be part of the cPanel function (I don't know, as I no longer use cPanels), or it may be some other code added by your or some other installed application.

One reason we're struggling here is that you are assuming that we (the readers and potential respondents) know when you are talking about URLs, when you are talking about filepaths, and understand the relationship between these two very different address spaces. This is not true, and leads to confusion and hesitance to even try to post a response.

When discussing these things, please state the requested URL(s), the filepaths to which they *should* resolve, and the filepaths to which they *do* resolve. If any misunderstanding is even remotely possible, then compare and contrast the expected and actual results.

If you get any kind of error, please post the error message from your server error log as well.

Following this procedure will help us help you. Otherwise, we are simply guessing at what your problem report means, what you might want, and why your code does not effect the desired result.

Anyway, the problem appears to be in the mapping of sub.olddomain.com URL-requests to the /sub filespace before your redirect is invoked, which leads to the /sub filepath being exposed to the client as part of the URL when the redirect is invoked.

If it turns out that this problem is caused by the cPanel code, one option is to map *all domains and subdomains* to /public_html. Once the requests arrive there, use your .htaccess file to first do all needed redirects (e.g. old URL replacement, index page canonicalization, and hostname canonicalization, in that order), and then after those redirects are done (or if none are needed), execute one or more internal rewrites to 'point' the requests for each canonical-hostname URL to the filespace where you store its files.

If it's not clear, this is an external client redirect:
 RewriteRule ^old-URL-path$ http://example.com/new-URL-path [R=301,L] 

and this is an internal rewrite:
 RewriteRule ^URL-path$ /filepath [L] 


If any internal URL-path-to-filepath rewrite in any server config file or .htaccess file is allowed to execute before an external client redirect, then the results of that internal rewrite (the modified internal filepath) will be made visible to that client (e.g. the "/sub" path will show up in the browser address bar).

Once this problem is fixed, you will likely appreciate why I try to find hosts that do not require the use of cPanel, and always pay for a unique IP address, even on shared hosting.

Jim

echoniner

6:52 am on Oct 25, 2010 (gmt 0)

10+ Year Member



Okay, let me explain:

First, in cpanel, I HAD

URL sub.domainnew.com setup to the FILEPATH public_html/sub

I also have

URL sub.domainold.com setup to same FILEPATH public_html/sub

Other than setting up the paths in cpanel, I am not using any of the other limited features in cpanel to control subdomains and such.

After more trial and error, I have found that, if I change it in cpanel such that:

URL sub.domainnew.com is setup to the FILEPATH public_html/sub

and

URL sub.domainold.com setup to merely FILEPATH public_html

then, with the following code in the .htaccess stored at FILEPATH public_html:


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


Then my problem is now solved! That is, now

http://sub.olddomain.com/index.php?variables


forwards successfully to

http://sub.newdomain.com/sub/index.php?variables


The trick was then setup the URL sub.olddomain to merely point to the FILEPATH public_html in cpanel.




My immediate problem solved, I moved to ensure odd ways to get in to subdomains didn't work. A few other problems cropped up, but let us look at this one, closely related to the above:

Remember, olddomain is an "add-on" domain residing at FILEPATH /public_html/olddomain on a shared host for the "primary domain" of newdomain.com. I need it to be setup as an add-on domain so I can setup email in cpanel, such as me@olddomain.com. Without this "add-on" setup, it also appears as a subdomain, namely:

URL olddomain.newdomain.com

In setting up the same code above in my .htaccess as follows:


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


I find that it does not forward and simply gives a 404 Not Found error. In the error log I get:

[Sun Oct 24 23:45:26 2010] [error] [client IP] File does not exist: /home/mylogin/public_html/olddomain/404.shtml
[Sun Oct 24 23:45:26 2010] [error] [client IP] File does not exist: /home/mylogin/public_html/olddomain/olddomain/


As you may notice, it is trying to direct to public_html/olddomain/olddomain/ again, but unlike with the situation above, I cannot setup the cpanel to the public_html folder, as the content and "add-on" domain setup requires I have the files for this domain in a subfolder at FILEPATH public_html/olddomain

So... if that is not terribly convoluted... any suggestions?

Is there a primer on setting all of this stuff up w/o using cpanel that you can point me to? Am I better off doing this? Should I not worry about all of the other options to get into my subdomains, and just disallow the links in the robots.txt, as this link suggests:

[guruofsearch.com...]

Thanks!

jdMorgan

12:11 pm on Oct 25, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



As I posted above, I would use your control panel to 'point' *all* domains and subdomains to /public/ and then put your own code into /public/.htaccess to further 'point' all these subdomains and old domains to the filespace used to store their files.

Because you're evidently on name-based shared hosting, using the control panel is required to set up the DNS and VirtualHost containers (and the e-mail), and there's likely no way to avoid that. But by pointing all domains and subdomain to the same .htaccess file, you can take care of the needed external redirects first, and then sort the requests for the different (sub)domains into the correct folders. This ensures that you can do the redirects and rewrites in the correct order and avoid having that "/sub" show up in your redirected URLs.

Since I've mentioned that before, here is a typical rule to point domains and subdomains to their own subdirectories, all stored under a master "/site-files" subdirectory. Note that this code is just an example, and the correct code for your sites is highly-dependent on exactly how you want to assign subdomains and domains to the server filespace:

RewriteCond $1 !^site-files/
RewriteCond %{HTTP_HOST} ^(www\.)?(([^.]+)+)\.com(\.?:[0-9]+|\.)$
RewriteRule ^(.*)$ /site-files/%2/$1 [L]

This maps www.domain.com and domain.com to /site-files/domain/ and it maps subdomain.domain.com and www.subdomain.domain.com to /site-files/subdomain.domain/

Jim

echoniner

6:45 pm on Oct 25, 2010 (gmt 0)

10+ Year Member



It seems I can still point my domains to the /public_html/ folder in cpanel and create emails and such for those "add-on" domains. So I tried your code above, making the following as my only text in a .htaccess file at /public_html/:


Options +FollowSymlinks

RewriteEngine On
RewriteCond $1 !^site-files/
RewriteCond %{HTTP_HOST} ^(www\.)?(([^.]+)+)\.com(\.?:[0-9]+|\.)$
RewriteRule ^(.*)$ /site-files/%2/$1 [L]


I did indeed move my sites files to a folder FILEPATH /public_html/site-files/olddomain/ and I pointed in cpanel olddomain to /public_html but it does not redirect, such that it still calls up the index.html residing at FILEPATH /public_html (which belongs to newdomain.com...because I only moved olddomain down below /site-files/ for this test).

Any ideas why?

echoniner

6:54 pm on Oct 25, 2010 (gmt 0)

10+ Year Member



my hunch is the above code does not work when www.olddomain.com points to a folder www.newdomain.com/olddomain (that is, it does not work with add-on domains)... am I right?

jdMorgan

7:36 pm on Oct 25, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You must point all domains to /public_html/

Then --as described above-- you must include all external redirects -- *all* of them, for all hostnames, in perpetuity -- in that one centralized /public_html/.htaccess file, and they must all be placed before the hostname-to-filespace-mapping code that I posted. (Please look for "the details" in the responses you get here; We would not mention them if they were not important, and because there is usually much more "help demand" here than "contributor supply" contributors are generally loathe to post them more than once. So please read carefully.)

Unfortunately, this solution is likely the best you can do on name-based shared hosting with "control-panel add-on domains." A more elegant or more flexible solution will require an upgrade to an IP-based shared host or better (i.e. a VPS or dedicated server).

Don't feel too bad, though, because we get a fair number of questions here from Webmasters whose control panels do not give them a choice as to where the "add-ons" will be located in their filespace, and this results in an essentially-unsolvable problem. There are still a few work-arounds available, but they are ugly, often leaving the door open for canonicalization vulnerabilities which can cause problems with search engines and which can be exploited by malicious competitors. So, it's actually a bit of luck that you can 'point' your (sub)domains to your filespace as you wish... otherwise, you'd be in with that lot.

Jim

echoniner

10:34 pm on Oct 25, 2010 (gmt 0)

10+ Year Member



JD, I apologize for my ignorance in this area, and beg your forgiveness if it seems I was cutting corners to tryout your fixes. I do very much thank you for your time.

Your approach sounds inviting, to have the kind of control you talk about from the .htaccess, bypassing the cpanel. I will consider it for the future. In the meantime, after using what I've learned here, thanks to your patient mentoring, I have solved all of my forwarding problems it seems.

In the above item, whereby I wanted URL olddomain.newdomain.com to forward to www.olddomain.com, it turned out I had to put this in a separate .htaccess at FILEPATH /public_html/olddomain/ (which I renamed /public_html/olddomain.com/ for clarity on my backend). The code I used was tailored from your advice here:


#
# -Disallow subdomain calls to this add-on domain:
#
RewriteEngine on
RewriteCond %{HTTP_HOST} ^olddomain.newdomain.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.olddomain.newdomain.com$ [NC]
RewriteRule ^(.*)$ "http\:\/\/www\.newdomain\.com$1" [R=301,L]


I cannot quite figure out at times why somethings in a .htaccess at FILEPATH /public_html/ affects the entire set of subdomains and such underneath, while others, like the code above, has to be placed specifically at /public_html/olddomain.com/ to work. However, with trial and error, I seem to have fixed all the redirects as I'd like them.




My only conern now is this canonicalization vulnerabilities. I think all of my code is straight forward, except for the following, which I used to redirect

URL www.newdomain.com/olddomain.com/


to

URL www.olddomain.com


(that is, to prevent someone from seeing the site from within newdomain.com, the hosted "primary" domain.)

I did this by placing in a .htaccess file at /public_html/olddomain.com the following code:


Options +FollowSymlinks
RewriteEngine On
Redirect /olddomain.com http://www.olddomain.com


It seems to work fine, I see no error messages in the log, etc. Am I at risk of anything here?

I'm worried about it because of what I read at [guruofsearch.com...] (based on your expertise). But from what I glean, it seems the risk with the above is an endless loop... which I do not find any hint of. I should not I do have this in the same .htaccess:


# prevent infinite loops in the .htaccess files following rewrite conditions
RewriteCond %{REQUEST_URI} ^/(stats/|missing\.html|failed_auth\.html|error/).* [NC]
RewriteRule .* - [L]


which is a snippet of code that I've always had in my .htaccess and I have no idea at this point where I got it.

Am I safe then from vulnerabilities? How do I identify an endless loop?

g1smd

11:33 pm on Oct 25, 2010 (gmt 0)

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




RewriteCond %{HTTP_HOST} ^olddomain.newdomain.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.olddomain.newdomain.com$ [NC]
RewriteRule ^(.*)$ "http\:\/\/www\.newdomain\.com$1" [R=301,L]


The above code will not redirect for
olddomain.newdomain.com:80
which is a valid request; remove the end anchor.

Escaping is not required on the target URL.

Escaping is required in the patterns.

Finally,

^olddomain.newdomain.com$ OR
^www.olddomain.newdomain.com$


logically simplifies to

^(www\.)?olddomain\.newdomain\.com


Note escaping and removed end anchor.

g1smd

11:37 pm on Oct 25, 2010 (gmt 0)

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



You have used RewriteRule and [R=301,L] for most of your rules. This is good.

One of your rules uses Redirect, and sends a 302 response. Alter this rule to use RewriteRule and add the [R=301,L] flags.

It is a very bad idea to mix Redirect/RedirectMatch and RewriteRule in the same site. If you use RewriteRule for any of your rules, you should use RewriteRule for ALL of the rules.

jdMorgan

11:42 pm on Oct 25, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



When a request arrives at your server, it gets "sent" to whatever DocumentRoot is defined for the hostname (often loosely/incorrectly called a domain name) specified in the request. When you "point" a "domain" in your control panel, the control panel writes a DocumentRoot directive (and several other things) into the server config file (which you can't access directly on shared hosting).

Now, only .htaccess files in that DocumentRoot directory and in directories below that directory will ever get executed for this request. That is why the location where you install a rule is important.

Note that this directory-path can change due to internal rewrites done by RewriteRules along the way, so you have to be very careful when analyzing this.

In a bit of a hurry again, but do not mix the use of Redirect and RedirectMatch, etc, from mod_alias with the use of mod_rewrite RewriteRules. It can cause problems. If you use mod_rewrite for anything, use it for everything. So re-code your "Redirect" as a RewriteRule to save yourself some possible trouble with unexpected directive execution order.

Jim

echoniner

12:48 am on Oct 26, 2010 (gmt 0)

10+ Year Member



g1smd: thanks for the advice. I've made all the changes.

Just to clarify, this should now be the format (and it should forward all links and variables beyond the URL):


Options +FollowSymlinks
RewriteEngine On
RewriteRule /olddomain.com http://www.olddomain.com [R=301,L]


No escaping and end anchors here, right?

echoniner

1:14 am on Oct 26, 2010 (gmt 0)

10+ Year Member



actually, just tested the above:

URL http://www.newdomain.com/olddomain.com/file.php


is intended, from the above, to forward to

URL http://www.olddomain.com/file.php


but it does not. Only the top-level call forwards. That is, only:

URL http://www.newdomain.com/olddomain.com/


forwards to

URL http://www.olddomain.com


What have I done wrong here? I've tried a variation of the above code with no success:


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

jdMorgan

3:24 am on Oct 26, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The HTTP_HOST variable contains only the requested hostname sent in the HTTP "Host" header, possibly in FQDN format (with a trailing period), and possibly including an appended port number (an example of both, and perfectly-valid, would be "www.newdomain.com.:80"). But it will not contain any URL-path info:

Options +FollowSymLinks
RewriteEngine on
#
# Redirect client requests for "/oldomain.com/" file storage directory path in newdomain.com's
# URL-space to olddomain.com's root URL-space (in other words, 'hide' the fact that
# olddomain.com's files are stored as a subdirectory in newdomain.com's filespace)
RewriteCond %{HTTP_HOST} ^(www\.)?newdomain\.com [NC]
RewriteRule ^olddomain\.com/(.*)$ http://www.olddomain.com/$1 [R=301,L]

If the comments are not correct, then neither is the code...

Jim

echoniner

6:37 am on Oct 26, 2010 (gmt 0)

10+ Year Member



Thanks again. However, for some reason, it didn't work, though you understood the problem precisely. I fiddled with the code and found this to work:

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


I placed this in the .htaccess underneath the FILEPATH /public_html/olddomain.com and it does not seem to affect the higher levels, where reside the files for newdomain.com.

Do I have any loops or vulnerability issues do you think?

echoniner

6:39 am on Oct 26, 2010 (gmt 0)

10+ Year Member



is there a safer way than the code I just offered above... b/c I'm worried about it...

jdMorgan

3:43 pm on Oct 26, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, I don't see any obvious problem.

But do not worry, that won't help anything.

Test thoroughly instead.

Make a fully-representative list of both "good" and "bad" URLs to be used in requests, then test your server's responses to all of these. The "Live HTTP Headers" add-on for Firefox and Mozilla-based browsers can very handy in this regard, as it will show you the HTTP transactions that occur between your browser and your server for each requested URL.

Make sure you get a single redirect when the tested URL is "bad" and that you get no redirects at all when the tested URL is "good."

Research and testing is far more productive than worrying -- Knowledge conquers doubt.

Jim

g1smd

4:09 pm on Oct 26, 2010 (gmt 0)

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



If you have a large number of URLs to test, paste them into a text file as a list, then import that list to Xenu LinkSleuth and generate a crawl report from that.

echoniner

2:24 am on Oct 27, 2010 (gmt 0)

10+ Year Member



Well I just wanted to say thank you again to both of you for your time and patients. I know quite a bit more about Apache htaccess coding than I did before.

Two knowledge questions so I can fully understand the code above, if I may:

What does the $1 at the end of a target link mean/do?

How about the ^(.*)$ ?

Example for both as in:

RewriteRule ^(.*)$ http://www.olddomain.com$1 [R=301,L]

g1smd

7:49 am on Oct 27, 2010 (gmt 0)

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



(.*) "captures" the path part of the URL.

$1 re-appends it.

Any query string (if present in the original request) is automatically re-appended without having to specify anything.

You probably need a / before the $1 too.

jdMorgan

12:22 pm on Oct 27, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The resources cited in our Apache Forum Charter may prove useful to you. In fact, they are rather critical to your long-term success using mod_rewrite...

Jim

echoniner

5:34 am on Oct 28, 2010 (gmt 0)

10+ Year Member



thanks!

echoniner

9:26 am on Nov 17, 2010 (gmt 0)

10+ Year Member



If I may: one more line of htaccess:

How to prevent requests for

[mydomain.com...]

but redirect them to

[sub.mydomain.com...]

?

I tried the logic as I figured it from above, but no dice. Here's what I tried:

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


where's the logic failing me here? i have this in a .htaccess at the top of my filepath public_html (that is, at the mydomain.com level)

jdMorgan

10:49 pm on Nov 17, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



HTTP_HOST variable cnotains only the client-requested hostname -- optionally in FQDN format (with a trailing period) and/or with an appended port number. It does not include any part of the URL-path.

For example, HTTP_HOST will contain only:
mydomain.com
www.mydomain.com
sub.mydomain.com

or the valid variations:

mydomain.com.
www.mydomain.com.
sub.mydomain.com.

mydomain.com:80
www.mydomain.com:80
sub.mydomain.com:80

mydomain.com.:80
www.mydomain.com.:80
sub.mydomain.com.:80

It will never contain the trailing slash or any additional URL-path info.


Therefore, you need to move the "sub" URL-path-part out of the RewriteCond pattern, and put it into the RewriteRule pattern:

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

Be aware that if you also rewrite requests for the URL [sub.mydomain.com...] to the filepath /sub/xyz then this new redirect rule and the pre-existing rewriting rule will interact, leading to an 'infinite' rewrite/redirect loop. The redirect rule needs to be made selective so that it only redirects direct client request for the subdomain's *filepath* and does not redirect previously-internally-rewritten requests. So if this is the case, then use

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /sub/
RewriteRule ^sub/(.*)$ http://sub.mydomain.com/$1 [R=301,L]

to solve that problem.

Note that this modified rule removes your original RewriteCond, and thus can also correct client requests for sub.mydomain.com/sub/xyz as well as for mydomain.com/sub/xyz and www.mydomain.com/sub/xyz, redirecting them all to sub.mydomain/xyz

Jim
This 34 message thread spans 2 pages: 34