-1

I need to redirect users with old links to a subdomain to a new link that is dynamically generated using information from the link.

For example:

Old Link will look like this:

subdomain-2013.old-domain.com/data/process/aqs-2d3f4f5g-fgtgyy-uunb.xml

To construct the new link, I need the year provided in the subdomain and the file name. (The year will change, it is not constant).

New Link:

new-domain.com/2013/data/process/aqs-2d3f4f5g-fgtgyy-uunb.xml

How is this done? Is this something accomplished with .htaccess?

Community
  • 1
  • 1
  • Does this answer your question? [Reference: mod\_rewrite, URL rewriting and "pretty links" explained](https://stackoverflow.com/questions/20563772/reference-mod-rewrite-url-rewriting-and-pretty-links-explained) – CBroe May 02 '22 at 12:43
  • @CBroe It's all new to me. I'll review it. It doesn't appear to cover how to extract the year from the subdomain. – Avalynn Circe May 02 '22 at 12:53
  • You will have to use a RewriteCond for that, to access the HTTP_HOST variable. Whatever parts of that your pattern captures, you can then refer to as back references with %1, %2 etc. in a following RewriteRule. – CBroe May 03 '22 at 06:10

2 Answers2

1

You can do it like this using mod_rewrite near the top of the root .htaccess file:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^subdomain-(\d{4})\.(example\.com) [NC]
RewriteRule ^data/process/([a-z0-9-]+\.xml)$ https://%2/%1/$1 [R=301,L]

Where %1 and %2 are backreferences to the preceding CondPattern and $1 is a backreference to the captured group in the RewriteRule pattern.

HOWEVER, I've just noticed you've tagged the question wpengine. The webhost "WP Engine" no longer supports .htaccess config files. Reference: https://wpengine.com/support/htaccess-deprecation/ - UPDATE: Although it seems this tag has since been removed from the question.


UPDATE: If the target domain is different (a later change to the question) then this would need to be hardcoded in the substitution string, replacing the %2 backreference. For example:

RewriteCond %{HTTP_HOST} ^subdomain-(\d{4})\.old-domain\.com [NC]
RewriteRule ^data/process/([a-z0-9-]+\.xml)$ https://new-domain.com/%1/$1 [R=301,L]

The example in the question is passing just the .xml filename to the target URL, it is not passing the preceding URL-path, which is what the rule above is doing.

MrWhite
  • 43,179
  • 8
  • 60
  • 84
  • The above solution does not work. It did not insert 'data/'process/' correctly and does not include the new domain. https://htaccess.madewithlove.com?share=12f40a06-7dec-4e8e-a59e-d082e8fa4534 – Avalynn Circe May 11 '22 at 15:23
  • @AvalynnCirce "It did not insert 'data/'process/' correctly" - But the example in your question does not insert `data/process/` either - the example you gave uses the "`.xml` filename" only from the requested URL and discards the preceding URL-path. "and does not include the new domain." - Your original question (before the edit) used the same domain as both the source and target. I've updated my answer to use the "new-domain". – MrWhite May 23 '22 at 00:12
  • You're right, sorry. I updated the question. – Avalynn Circe May 24 '22 at 03:04
  • @AvalynnCirce You appear to have tried to edit the question whilst "logged out" (as an "Anonymous user")?! Consequently, the edit is still "pending" as it needs approval from other users. – MrWhite May 25 '22 at 00:09
0

I found two sites that were instrumental in solving this problem and then testing the solution, linked below.

Here is the solution:

 RewriteCond %{HTTP_HOST} ^.*(\d{4})\.(.*?)$
 RewriteRule . http://www.new-domain.com/%1%{REQUEST_URI} [R=301,L]

https://regex101.com/r/XFMOwW/1

https://htaccess.madewithlove.com?share=3ea90fee-86df-488e-9346-b0797c26a3f1

  • But this doesn't solve the problem stated in the question? In the Q you state you just need the "file name" (ie. the last path segment) and you discard the preceding URL-path in your example. If this should apply to any file type and any URL-path then this should be communicated in the question. Your regex `^.*(\d{4})\.(.*?)$` that matches against the `Host` header can be simplified to just `(\d{4})\.` - but this arguably matches too much. Why does it need to be so generic? Are not all requests going to `old-domain.com` (as stated in the Q)? Generally, regex should be as specific as possible. – MrWhite May 23 '22 at 10:13
  • oh jeesh, my mistake. I updated the question with the correct criteria. – Avalynn Circe May 24 '22 at 03:04