1

...the MAYBE-dup answer does solve my problem, but the question doesn't.

Do TWO things:

  1. Keep subdomain before

  2. Keep path after

I can't find ONE .htaccess or Apache config that does BOTH anywhere on SE or elsewhere on the web:

Via Apache .htaccess rewrite, forward the old domain, but keep BOTH the same subdomain AND path/after.

From:

samesubdomain.domain1.tld1/same/path

To:

samesubdomain.domain2.tld2/same/path

I have many subdomains AND trailing addresses. I am migrating an old site, but need my old subs and their pages to still work on the new domain.


Not-but-close duplicates: (The answers address my problem, but the question itself does not describe my problem, but this solved my problem also.)

Redirect wildcard subdomain to same subdomain on different domain


Courtesy the MAYBE-dup answer, for the subdomain, I currently have:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.+\.)?domain1.com$ [NC]
RewriteRule ^ http://%1domain2.com%{REQUEST_URI} [R=301,L]

...now, I need to add the/path half WITHOUT conflicting with this code.

  • 1
    This should be rather easy. What is the part you are having a problem with? – Michael Hampton Jun 18 '19 at 05:37
  • My problem is that every question or tutorial on the web partially relates, but 1: nothing I find forwards to a new domain while preserving all subdomains. Plus, 2: I need a way that preserves the path after without conflicting with the first. Instead, everything swaps subdomains with paths or only preserves paths. I can't find 1 and I need to know how 2 should work so it won't conflict with the method of 1. Feel me? – Jesse יִשַׁי Jun 18 '19 at 05:41
  • Possible duplicate of [Redirect wildcard subdomain to same subdomain on different domain](https://serverfault.com/questions/691524/redirect-wildcard-subdomain-to-same-subdomain-on-different-domain) – MrWhite Jun 18 '19 at 09:09
  • @MrWhite Thank you, but... That MAYBE-dup is half-helpful, but I also need the PATH after. My concern is that I can't conflate two codes. A rewrite that works for the subdomain might have an "arguments" conflict with a rewrite for the path after. Am I making sense? – Jesse יִשַׁי Jun 18 '19 at 10:11

1 Answers1

1

Although the linked question doesn't specifically state this, the code does actually copy the URL-path (and any query string) as well. Although the answers are admittedly lacking in any explanation.

Assumptions:

  • There is always a subdomain. So, you don't want to redirect the domain apex. ie. domain1.tld1 (with no subdomain) does not redirect to domain2.tld2.

Near the top of your root .htaccess file:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^([^.]+)\.domain1\.tld1 [NC]
RewriteRule (.*) https://%1.domain2.tld2/$1 [R=302,L]

The RewriteCond directive captures the subdomain from the request. The %1 backreference in the RewriteRule substitution is a backreference to the captured subdomain (ie. ([^.]+)).

The $1 backreference contains the URL-path, captured by the RewriteRule pattern, ie. (.*).

Note that I specifically omitted the trailing $ (end-of-string anchor) on the CondPattern in order to match FQDN, that end in a dot.

Any query string is passed through unchanged.

Note that this is currently a 302 (temporary) redirect. Always test with 302s to avoid caching issues. If you've previously tested with 301s then you will need to make sure you've cleared your browser cache.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.+\.)?domain1.com$ [NC]
RewriteRule ^ http://%1domain2.com%{REQUEST_URI} [R=301,L]

...now, I need to add the/path half WITHOUT conflicting with this code.

The code already contains the URL-path part. That's what the %{REQUEST_URI} part is in the RewriteRule substitution string. The REQUEST_URI server variable contains the full URL-path. This is just another way of doing the same thing as I did above - I used a backreference $1 instead. (Note that the REQUEST_URI server variable contains the slash prefix, whereas the backreference I used does not, hence the difference in slashes in the substitution string.)

This code is missing a backslash escape on the literal dot in .com.

Note that this solution makes the subdomain optional. It will redirect domain1.com (no subdomain) to domain2.com.

MrWhite
  • 11,643
  • 4
  • 25
  • 40
  • I've updated my answer, to explain the differences with the code in your question. – MrWhite Jun 18 '19 at 10:34
  • 1
    I like your answer because it explains why the dup is confusing, also explaining the "optional subdomain" part, as well as the two other ways of getting the code. SO... should we keep this question here or should I delete the question as a dup? What do the people want? – Jesse יִשַׁי Jun 18 '19 at 10:40
  • I would keep it now (I've retracted my close vote) - as you say, the linked question doesn't specifically address the URL-path (that's not the focus of the question) and they don't offer any explanation (unfortunately common with mod_rewrite related answers), so they don't necessarily help future readers so much. – MrWhite Jun 18 '19 at 10:52
  • Thanks for explaining. As the webmaster, it's important that Questions ask for their Answers. I don't want to use the "wrong" code just because it works when it shouldn't, if you get me. So, I'll leave it up. This was good all around. – Jesse יִשַׁי Jun 18 '19 at 10:53