Redirect to old site if content is not found

Viewed 68

We have recently deployed new site as the old site was giving us a lot of problems and its a news site. We have changed the old site to old.domain.com and made the new site as domain.com. Before people used to visit old site on domain.com which is now shifted to old.doamin.com. To shift the content from old site to new is not possible.The site was 4 years old and a lot of content were shared from the site like domain.com/xyz/abc but when now people come to visit those old shared link they will be directed to new site domain.com/xyx/abc where the content will not be found as the content is now shifted to old.domain.com/xyz/abc. So I want to achieve redirection so that when the people click old shared link domain.com/xyz/abc they will be redirected to old.domain.com/xyz/abc only changing domain so that the content can be displyed from old site. Site is hosted in apachel in cpanel.

I have tried the following in htaccess

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*) https://old.domain.com/$1 [P]

</IfModule>

P.S I am not a programmer but I am trying to achieve this for my company.

1 Answers

You can use this rule to redirect URLs from domain.com/foobar to old.domain.com/foobar .

RewriteEngine on

RewriteCond %{HTTP_HOST} ^domain\.com$
RewriteRule . https://old.domain.com%{REQUEST_URI} [R=302,L]

This will redirect all requests received on example.com to old.example.com . If you have some files and folders on the domain.com that should not be redirected then use the following instead :

RewriteEngine on

RewriteCond %{HTTP_HOST} ^domain\.com$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . https://old.domain.com%{REQUEST_URI} [R=302,L]

If this works then change R=302 to R=301 to make the redirection permanent.

Related