2

I have recently stopped using a subdomain for my blog, i need to forward all links that use that domain to the same link path but on my main domain.

Example being:

  • blog.example.com/blog/sales/blog-title

needs to redirect to :

  • www.example.com/blog/sales/blog-title

I can redirect the subdomain itself so

-blog.example.com/

goes to

  • www.example.com/blog

but as soon as I try to redirect a page it doesn't redirect and doesn't load. Can anyone shed some light on it? I currently have this in my htaccess for my main redirect:

RewriteCond %{HTTP_HOST} ^blog\.example\.com [NC]
RewriteRule (.*) http://www.example.com/blog/$1 [L,R=301]

I have other subdomain redirects going on but none wildcard redirect.

Amit Verma
  • 40,709
  • 21
  • 93
  • 115
A Virtue
  • 31
  • 1
  • 4

1 Answers1

3

In your example, you want to redirect from

blog.example.com/blog/sales/blog-title

to

www.example.com/blog/sales/blog-title

But in your rule, you insert another subdirectory blog in the substitution part, which gives

www.example.com/blog/blog/sales/blog-title

instead, and an error 404 as a result.

To redirect from one domain to another with the exact same request path, use

RewriteCond %{HTTP_HOST} ^blog\.example\.com$ [NC]
RewriteRule ^ http://www.example.com%{REQUEST_URI} [L,R]

When it works as it should, you may replace R with R=301. Never test with R=301.

Community
  • 1
  • 1
Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198