简体   繁体   中英

How do I do a proper 301 redirect from subdomain to root using .htaccess

Scope

Currently working on moving a client's website away from HubSpot and sticking solely with WordPress as our CMS / Blogging platform. For those unfamiliar with HubSpot, it's COS platform ends up taking over about 1/3 of your site in the form of blog posts, calls to action, analytics, etc. Blog posts then are published through HubSpot, through a subdomain that's pointed directly at HubSpots servers. So instead of example.com/blog, it would be something like info.example.com/blog.

Details

HubSpot domain is info.example.com, while root domain for WP site is www.example.com. I'm trying to redirect the subdomain info.example.com and all related posts to example.com strictly in the .htaccess file.

I've already created a 301 redirect in the DNS panel for the subdomain, so the last part is getting the blog post URL's listed out in .htaccess so we don't lose SEO juice. In total, there's about 20 URL's to be redirected.

In .htaccess, I have them written as such:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^info.example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

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

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

..etc etc. The root domain does not use /blog in it's URL structure, so don't confuse it with a typo :)

Can anyone tell me if there's a glaring error in how these redirects are written?

Thanks in advance and let me know if more info is required.

Holy shit I figured it out. The error was on WP Engine's side. The info.example.com domain was pointed to the hosting account, but it wasn't listed in the domain manager.

After confirming the subdomain on WP Engine's side, I pointed it to the www domain, and now all links both redirect properly and carry SEO juice with them.

FTW!

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