Rastalamm
Rastalamm

Reputation: 1782

301 redirect using htaccess for blog pages and all other pages

I am trying to redirect an old website to a new website. I see the old site as two parts. The blog articles and everything else. I would like the blog articles to be redirected to their new locations on the website, while keeping the same URL (more or less). I would like everything else to redirect to the new site's home page.

Requirements:

  1. All blog posts should redirect to their same URL (without the /blog part) but on the new domain
  2. All non-blog pages should redirect to a single new page

Example:

Upvotes: 0

Views: 3012

Answers (2)

MrWhite
MrWhite

Reputation: 45829

I assume different physical servers?

At the top of your .htaccess file on the old site, try the following:

RewriteEngine On
RewriteRule ^(blog)(/.*)? http://$1.newsite.com$2 [R=302,L]
RewriteRule .* http://www.newsite.com/ [R=302,L]

This first redirects the "blog" URLs then redirects everything else. The order is important.

Change the 302 (temporary) redirect to a 301 (permanent) when you are sure it's working OK. (302 redirects won't be cached by the browser - in case of an error.)

However, I would recommend not redirecting multiple pages to a single home page on the new site if you can help it. This is generally a bad experience for users and Google is likely to treat it as a soft-404 and ignore it anyway.

Upvotes: 1

Mohammed Elhag
Mohammed Elhag

Reputation: 4302

Put the following code @ oldsite , main directory, .htaccess file:

RewriteEngine on

RewriteRule !^blog ($|/) http://www.newsite.com%{REQUEST_URI} [L,R=301]

# above line will redirect entire site to new site and exclude blog directory.

Then ,put the following code @ the oldsite.com/blog/ directory .htaccess file :

RewriteEngine On
RewriteRule ^(.*)$ http://www.blog.newsite.com/$1  [L,R=301]

# above line will redirect all requested url for oldsite.com/blog/ directory
# to blog.newsite/ with same extensions and if you remove $1 it will
# be redirected to index @ blog.newsite.com

If there are some blog/SubDirectories/ , check if there are no .htaccess in them , or they existing but has nothing , so no problem but if they have some rules ,Make sure that it is adapted to not effect /blog/ directory .htaccess rules

Upvotes: 1

Related Questions