Redirect subdomain to domain with .htaccess in dom

2019-09-09 07:47发布

问题:

My webpage used to be blog.example.com, but it is now example.com. I had to delete the /blog directory (where the website hosted blog.example.com), because I have a Wordpress page at the URL example.com/blog and that screws it up.

I have tried:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^blog\.example\.com
RewriteRule ^(.*)$ http://www\.example\.com/$1 [L]

As well as:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?blog\.example\.com$ [NC]
RewriteRule ^ http://www.example.com/%{REQUEST_URI} [NE,R=301,L]

And:

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

But none of these make any difference in the subdomain. What can I do to redirect blog.website.com/URLS to website.com/URLS? (without creating example.com/blog because it will mess up Wordpress's webpage website.com/blog)

回答1:

I figured it out! I had to:

  1. Remove the redirect from bluehost that was supposed to redirect the subdomain to the main domain
  2. Create the subdomain blog.example.com at the directory at public_html/blog-redirect
  3. Add this redirect code to the .htacces file under /blog-redirect:

    RewriteEngine On
    RedirectMatch 301 ^/(.*)$ http://example.com/$1