How can I 301 redirect specific subdomain URLs using .htaccess?

20,033

Solution 1

try this:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^sub\.domain\.com$ [NC]
RewriteRule ^(.*) http://newsub.domain.com/ [L,R]

it works on my side

Solution 2

Here's what I ended up doing:

# first re-write all foo.example.com requests to bar.example.com
RewriteCond %{HTTP_HOST} ^foo\.example\.com [NC]
RewriteRule (.*) http://bar.example.com/$1 [L,R=301]

# now re-write each individual URL
RewriteRule ^this-is-mypage /this-is-really-my-page [NC,L,R=301]
Share:
20,033
Finch
Author by

Finch

Updated on April 22, 2020

Comments

  • Finch
    Finch about 4 years

    I need to redirect particular URLs on a subdomain to completely different URLs on a different subdomain. For example:

    http://foo.example.com/this-is-my-page
    

    Needs to 301 to:

    http://bar.example.com/this-is-really-my-page
    

    I’ve tried setting up a simple Redirect 301 in .htaccess but it doesn't seem to work. For example:

    Redirect 301 http://foo.example.com/this-is-my-page http://bar.example.com/this-is-really-my-page