Home > Mobile >  Redirect dynamic subdomain URL to its dynamic site URL
Redirect dynamic subdomain URL to its dynamic site URL

Time:05-10

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/aqs-2d3f4f5g-fgtgyy-uunb.xml

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

CodePudding user response:

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]

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/

CodePudding user response:

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

  • Related