Lucian Ilea
Lucian Ilea

Reputation: 137

htaccess redirect old links to new website index and manually redirect some exceptions

We have created a new website which has a different page structure that the old one. We still want to have the contact page pointing to the contact page on the the website, the team page to the new one, and so on.

The new website is on another domain and the links are not the same - for example on the old website we had www.example.com/contact and now we have www.newexample.com/contact-us.

I would like to be able to do manual redirects for the important pages like this

Redirect 301 old_link new_link

And all the pages that are not covered in the list of manual redirects should be redirected to the new website's index.

Is this possible, or is it a better approach?

Thank you

Upvotes: 2

Views: 2153

Answers (3)

LucasF
LucasF

Reputation: 903

You can set up this rule in your old document root:

RewriteEngine On
#Redirect to new structure
RewriteRule ^contact$ http://www.newexample.com/contact-us [L,R=301]

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

You need to set this for the new site:

#Redirect all 404 Errors to http://www.newexample.com
ErrorDocument 404 http://www.newexample.com

Upvotes: 1

EngineerCoder
EngineerCoder

Reputation: 1455

Another method: Redirect all request to a php file using .htaccess an manage via php

<?
$url=$_SERVER['QUERY_STRING'];
$start= strrpos($url,'/')+1;

if ( strpos($url,"/old_structure/")!==false ) $newest="/new_structure";

$url=substr($url,$start);
$url=trim($url);
$domain="domain.com". $newest ."/$url";



Header( "HTTP/1.1 301 Moved Permanently" ); 
Header( "Location: http://$domain" ); 
die();
?>

Upvotes: 0

Ankit Srivastava
Ankit Srivastava

Reputation: 256

you can put a Rewrite condition for each of the old urls like RewriteCond %{REQUEST_URI} !(old_link) [NC] and then put a Rewrite rule that serves all other links RewriteRule ^(|/|/(.*))$ new_link [R=301,L]

Upvotes: 0

Related Questions