$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>htaccess - Redirect Specific Wildcard Subdomain to a specific URL on another domain|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

htaccess - Redirect Specific Wildcard Subdomain to a specific URL on another domain

matteradmin9PV0评论

I want to redirect specific wildcard subdomain to a specific URL on another domain. Both domains are on the same server/public_html, and redirect all the inner pages of the wildcard subdomain to the inner pages of the other domain.

For example:

  • redirect music.example to another.example/music/

and

  • redirect music.example/happy-birthday/ to another.example/happy-birthday

  • redirect music.example/sing-to-you/ to another.example/sing-to-you/

I have more than 100k posts so I cannot make the redirection of the inner pages one by one using .htaccess.

I want to redirect specific wildcard subdomain to a specific URL on another domain. Both domains are on the same server/public_html, and redirect all the inner pages of the wildcard subdomain to the inner pages of the other domain.

For example:

  • redirect music.example to another.example/music/

and

  • redirect music.example/happy-birthday/ to another.example/happy-birthday

  • redirect music.example/sing-to-you/ to another.example/sing-to-you/

I have more than 100k posts so I cannot make the redirection of the inner pages one by one using .htaccess.

Share Improve this question edited Mar 12, 2019 at 14:51 MrWhite 3,8911 gold badge20 silver badges23 bronze badges asked Mar 12, 2019 at 6:58 XATAXATA 6712 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

You can do something like the following at the top of your .htaccess file in the document root using mod_rewrite before the WordPress front-controller.

Note that since you have two distinct patterns (root directory to /music and everything else to the root directory on the other domain) then you'll need to two rules.

RewriteEngine On

# "music.example/" to "another.example/music/"
RewriteCond %{HTTP_HOST} ^music\.example\ [NC]
RewriteRule ^$ https://another.example/music/ [R=302,L]

# "music.example/<something>" to "another.example/<something>"
RewriteCond %{HTTP_HOST} ^music\.example\ [NC]
RewriteRule (.+) https://another.example/$1 [R=302,L]

The RewriteCond (condition) directive checks the requested host. The RewriteRule naturally redirects to the other domain. In the second rule the URL-path from the request is captured in the $1 backreference.

Note that this is a 302 (temporary) redirect. If this is intended to be permanent, then change to 301 (ie. R=301) but only once you have confirmed that it works OK. 301s can be problematic when testing as they are aggressively cached by the browser.

Post a comment

comment list (0)

  1. No comments so far