Redirecting Urls from old website to New - .htaccess

I am trying to redirect my website from old domain to new domain using htaccess. Both the websites have same pages with same url structure i only need to redirect the main url part i-e https://test.com old url to https://test1.com with other pages url as well. For example if i had a page in old website (https://test.com/page1) it should be redirected to (https://test1.com/page1). I used this code to redirect it but its not working any help.
Redirect 301 /page1/ https://test1.com/page1
Redirect 301 https://test1.com

You might find these helpful:
#Redirect from old domain to new domain
RewriteEngine on
RewriteBase /
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]
and
#Redirect from old domain to new domain with full path and query string:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(.*) http://www.newdomain.com%{REQUEST_URI} [R=302,NC]
Feel free to follow the resource: https://gist.github.com/ScottPhillips/1721489

The first Redirect has a different source and target URL-path. The old URL has a trailing slash and new one has not.
Then any request beginning with URL-path will return a redirect request to the client at the location of the target URL. Additional path information beyond the matched URL-path will be appended to the target URL.
This means, if you request
http://old.example.com/page1/some/script.php
the path beyond /page1/ (some/script.php without leading slash) will be taken and attached to the target /page1
http://new.example.com/page1some/script.php
To have the complete path appended, you must omit the trailing slash in the old URL-path, e.g.
Redirect /page1 https://test1.com/page1
To address the second Redirect, this will only work inside a Location directive, e.g.
<Location "/page1">
Redirect https://test1.com/page1
</Location>
Finally, when everything works as it should, you may change the status code to 301. Never test with 301.

Related

Redirect ALL pages from an old Domain to a new page on a different domain

I am trying to use htaccess to redirect ALL pages from a domain to a specific page on a new domain. Yes, I completely understand we will loose SEO value this way.
I currently have a cpanel redirect that makes this url:
https://www.kiss1047.net/
go to this
https://mytown-media.com/stations/kiss-104-7-kxnc-fm/
but that doesn't get any of the internal pages to redirect. I would also like all internal pages (here is an example):
https://www.kiss1047.net/listen-live
to also go to:
https://mytown-media.com/stations/kiss-104-7-kxnc-fm/
I have tried a few things, but they always carry over the page url, ie above /listen-live/
https://mytown-media.com/stations/kiss-104-7-kxnc-fm/listen-live/
and that results in a 404.
is there some htaccess magic i can employ here?
In your .htaccess file .. Make a single entry that looks like:
Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) https://mytown-media.com/stations/kiss-104-7-kxnc-fm/ [R=301,L]
This will direct ALL TRAFFIC (.*) to your other website .. Regardless of file name or directory .. And will not append the file/directory to the end of the URL .. IE listen-live
This is a 301 Permanent redirect [R=301,L] .. Which means once followed by Google, will be indexed as such .. Also will cache in users browsers so that the browser remembers the 301 instead of bouncing between websites as well.
This command in .htaccess redirects every page of your old domain to new domain's one specific URL.
RewriteEngine on
RewriteRule ^(.*)$ http://www.newdomain.com/ [R=301,L]
For your case:
RewriteEngine on
RewriteRule ^(.*)$ https://mytown-media.com/stations/kiss-104-7-kxnc-fm/ [R=301,L]
In result:
https://www.kiss1047.net/listen-live
will be redirected to:
https://mytown-media.com/stations/kiss-104-7-kxnc-fm/

301 Redirection from old archive link to new

I changed my blog archive links from https://example.com/2019/09/ to https://example.com/blog/2019/09/. I would like to redirect from old url to new one. Is there any way to redirect all archive url along with /blog/ slug. Currently old urls goes to 404.
Try the following at the top of your .htaccess file:
RewriteEngine On
# Redirect "/NNNN/NN/" to "/blog/NNNN/NN/"
RewriteRule ^\d{4}/\d\d/$ /blog/$0 [R=302,L]
The $0 backreference contains the entire URL-path that is matched by the RewriteRule pattern (1st argument).
Change the 302 (temporary) redirect to 301 (permanent) only once you have confirmed that it works as intended.
Assuming this is WordPress then you don't need to repeat the RewriteEngine On directive, since it probably already occurs later in the file.

.htaccess: Redirect from a.com/example to b.com/example?param1=foo&param2=bar

I'm trying to redirect from an old domain to a new one, and append some GET parameters to the redirect URL. If a subdirectory is specified with the old domain, this subdirectory should also be included in the redirection.
For example:
a.com
should redirect to:
b.com?param1=foo&param2=bar
And:
b.com/example
should redirect to:
b.com/example?param1=foo&param2=bar
I have found some examples for redirecting to another domain, but I was never able to append the extra GET parameters.
You can use this single rule in .htaccess or Apache config of old domain.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?a\.com$ [NC]
RewriteRule ^ http://b.com%{REQUEST_URI}?param1=foo&param2=bar [L,R=301,NE,QSA]
QSA is used to preserve existing query parameters in old URL.

301 Redirect Problems

I have a url structure that is passed through as following
www.mysite.com/path1/path2/pageA.html - www.mysite.com/newpath/pageA.html
www.mysite.com/path1/path2/path3/pageB.html - www.mysite.com/newpath/path3/pageB.html
www.mysite.com/path1/pageC.html - www.mysite.com/newpath/pageC.html
I have made the changes for the pass through like this and it is working
RewriteRule ^/newpath/(.*) /path1/path2/$1 [PT,L]
RewriteRule ^/newpath/(.*) /path1/$1 [PT,L]
I have also made changes in code to have the new URL's in pages except for some pages where we need 301 redirects to the new structure
Actual Location
www.mysite.com/path1/path2/pageA.html
www.mysite.com/path1/path2/path3/pageB.html
URL path in hyperlinks that have to be 301 redirected to Final Path URL
www.mysite.com/newpath/path2/pageA.html
www.mysite.com/newpath/path3/pageB.html
Final URL Path
www.mysite.com/newpath/pageA.html
www.mysite.com/newpath/path3/pageB.html
I am not able to create 301 redirects with the current pass through changes present as mentioned above.
You need to remove leading slash from URI matching pattern in .htaccess:
RewriteRule ^newpath/(.+)$ /path1/$1 [NC,L]

Problems with htaccess Redirect (Page on one domain to same page on another)

I'm trying to redirect a few pages to a new domain and I have done this before but for some reason I can't get the code to work.
RewriteEngine On
Redirect 301 http://domain.com/page1.html http://domain2.com/page1.html
Can anyone see where I'm going wrong?
In .htaccess file the below code will ensure that all your directories and pages of your old domain will get correctly redirected to your new domain.
The .htaccess file needs to be placed in the root directory of your old website
Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]
Since you say you only want to direct a FEW of your pages and not all of them, you can do:
RewriteEngine On
Redirect /~myaccount/oldpage.html http://www.newsite.com/newpage.html
You specify the path to the page on your current server followed by the URL to redirect to.
OR you can do:
RedirectMatch 301 ^/oldpage\.html$ http://www.newsite.com/newpage.html

Resources