Multiple website redirects - .htaccess

I am looking to redirect and old website to a new one, but we want to do it in 2 parts;
We want to redirect the whole blog to the new blog page
http://www.websiteA.com/news
http://www.websiteA.com/news/post1
http://www.websiteA.com/news/post2 ...
Redirects to;
http://www.websiteB.com/news
We then want to redirect everything else to the homepage on the new website.
http://www.websiteA.com/page1
http://www.websiteA.com/page2
http://www.websiteA.com/page3
Redirects to;
http://www.websiteB.com/
How would I do this through the .htaccess?

You can use these 2 redirect rules in your site root .htaccess:
RewriteEngine On
# news URIs
RewriteRule ^(news)(?:/.*)?$ /$1? [L,NC,R=301]
# remaining ones
RewriteRule . /? [L,NC,R=301]

Related

.htaccess redirect for https://.co.nz website to .com version(including all subpages)

I am palling to migrate my website from the .co.nz to the .com but need to setup a 301 redirect so all of the individual pages will still be routed properly without any 404 pages from .co.nz version
All https://
I want "https://www.example.co.nz/" to go to "https://www.example.com"
Which will also redirect
https://www.example.co.nz/contact to go to https://www.example.com/contact
https://www.example.co.nz/men to go to https://www.example.com/men
Many Thanks
This should do the trick:
Redirect 301 / https://www.new-example.com/
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !new-example.com$ [NC]
RewriteRule ^(.*)$ https://new-example.com/$1 [L,R=301]
You put this in the .htaccess file of the .co.nz domain. This will redirect all your content and subfolders to their appropriate subfolders on the new domain (so example.me/subfolder to example.com/subfolder) AND also force redirect to https part.

Htaccess rewrite rule with exception for home page

This is the current rule in the htaccess
# Redirect
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ http://new.com/$1 [R=301,L]
</IfModule>
Works fine so far but it should have one exception: if it is the home page it should redirect to a specific url. So if its http://old.com/ it should redirect to http://new.com/shop in every other case it should behave like above so http://old.com/cat/xyz redirecting to http://new.com/cat/xyz and so on.
Any suggestions?
.* in regex matches everything including landing page URI i.e. / but .+ skips landing page.
You can use these rules:
RewriteEngine On
# handle landing page
RewriteCond %{HTTP_HOST} ^(?:www\.)?old\.com$ [NC]
RewriteRule ^$ http://new.com/shop [R=301,L]
# everything but landing page
RewriteCond %{HTTP_HOST} ^(?:www\.)?old\.com$ [NC]
RewriteRule . http://new.com%{REQUEST_URI} [R=301,L,NE]
301 a single page URL to another
To create a 301 redirect from one URL to another URL, add the
following line of code:
Redirect 301 /retiredpage.html http://www.example.com/newpage.html
You can add as many of these redirect lines as necessary to the .htaccess file.
301 a directory URL and all of its contents to another
If you have redesigned your site architecture and renamed a directory, you need to create a 301 for the entire directory. Here’s how:
RedirectMatch 301 ^/oldname/ http://www.example.com/newname/
301 a domain name URL to another
If you just bought an aged domain name whose traffic (and search page rank value) you want to use to augment that of your existing site’s domain, you can set up a 301 to transfer all traffic and ranking from the purchased domain name to your current site.
Use the following code as an example:
RedirectMatch 301 ^(.*)$ http://www.example.com
Be sure you set up this redirect code in the .htaccess file of the source site you want redirected, not the redirect target site!
301 domain name URL variants for canonicalization
Since search engines index URLs, having multiple URLs in the index that point to the same content page divides the available page rank credit for that page among those URLs. This is definitely a “not optimized for search” state of affairs! To learn more about the details of canonicalization, take a look at the Search Engine Land post Why Canonicalization Matters From A Linking Perspective. The bottom line is you want to consolidate the page rank to one (canonical) URL to optimize the search value of that content.
Once you understand canonicalization best practices, you’ll want to implement them on your site. That means you must account for all redirecting possible alternative URL variations to the canonical URL. Use the following code sample for your site’s home page:
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*(default|index)\.(html|php|htm)\ HTTP/ [NC]
RewriteRule ^(([^/]+/)*)(default|main|index)\.(html|php|htm)$ http://www.example.com/$1 [L,R=301]
The first two-line block of code redirects URLs that have omitted the “www.” prefix to the full “www.example.com” home page URL. That means the home page URL
**
http://example.com will not resolve on its own, but instead will redirect to http://www.example.com/.
**
The second code block redirects URLs specifying default page references to the URL that omits default page reference names. This code ensures that any home page URL that includes several versions of explicit page name references, such as default.htm or index.html, will be redirected to the canonical home page URL,
http://www.example.com/.

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
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
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]
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();
?>

how to redirect the url in htaccess with few exceptions

I want to redirect all URL's of my old site to new except few links. How can I do this using htaccess?
Except for these links
a) http://localhost/track/click_thru.asp
b) http://localhost/track/view_tracker.asp
c) http://localhost/track/pass_thru.asp
all other links on the site should be redirected to my new site.
Try adding these rules to the htaccess file in your old site's document root:
RewriteEngine On
RewriteCond $1 !^track/(click_thru|view_tracker|pass_thru)\.asp
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
The www.example.com is your new site. You can remove the $1 from the end of the rule if you don't want the path to be included in the redirect (e.g. going to /blah on old site redirects to http://www.exmaple.com/blah).

301 Redirect in .htaccess

I need to redirect my website to a new domain name but I want it to also redirect all of the pages. I want www.myoldsite.com/ to redirect to www.mynewsite.com/ but I also want all of the pages to redirect.
For example: www.myoldsite.com/faq to redirect to www.mynewsite.com/faq and so forth and so on. I know how to do it by doing each individual page but I have a lot of pages. How do I rewrite the 301 for it to basically change the root domain name but keep the directories and pages the same? Thanks a million!
You just need this in the htaccess file in your old domain's document root:
Redirect 301 / http://www.mynewsite.com/
If both your old site and new site share the same document root, then you need mod_rewrite instead:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?myoldsite\.com$ [NC]
RewriteRule ^(.*)$ http://www.mynewsite.com/$1 [L,R=301]

Resources