简体   繁体   中英

How do I make this work using .htaccess - redirect main domain to subdomain & redirect all other pages to new domain

I have example.com which I was able to redirect to home.example.com

Using:

RewriteEngine On
RewriteRule (.*) https://home.example.com/$1 [R=301,L]

But the other part I'd like to be able to do is to redirect ALL the other URLs to a new domain.

Like https://example.com/interestingblogpost to redirect to https://another.example/interestingblogpost .

Any advice, I couldn't seem find anything in my searches.

Oh and this is for a WordPress site.

Your current directives already redirect ALL URLs to another hostname (a subdomain of the main domain).

If you need to make specific redirects then you simply need to place these before your current directives. ie. most specific redirects should be first.

For example:

RewriteEngine On

# Specific redirects...
RewriteRule ^interestingblogpost$ https://another.example/$0 [R=301,L]

# Redirect everything else to home.example.com
RewriteRule (.*) https://home.example.com/$1 [R=301,L]

Assuming this .htaccess file only serves example.com , then the above would redirect https://example.com/interestingblogpost to https://another.example./interestingblogpost .

$0 is a backreference for the entire matched pattern.

You will need to clear your browser cache before testing as the earlier 301 that redirects everything will likely have been cached (if you had previously requested /interestingblogpost ). For this reason it is often preferable to first test with 302 (temporary) redirects that are not cached by default.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM