Redirect old domain backlinks to new domain - regex

I am releasing an updated version of one of my websites soon on a new domain name and I was wondering how I can make it so that the old urls point to my new website as it has quite a lot of valuable backlinks.
I have read about how to make it 301 redirect in .htaccess but I need something a bit more advanced as some of my url structures have changed. See the examples below.
olddomain.com
newdomain.com
- These will do just fine with the 301 redirect
olddomain.com/author/username
newdomain.com/user/username
- Different URL structure but I want the old urls to redirect towards the new url
olddomain.com/add
newdomain.com/submit
- Different page name for the same functionaly, would like to have these redirect correctly as well
I have no clue how to go about creating these last two redirects so if anyone could point me into the right direction that would be great! :)

Put this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} olddomain\.com [NC]
RewriteRule ^$ http://newdomain.com/ [R=301,L]
RewriteCond %{HTTP_HOST} olddomain\.com [NC]
RewriteRule ^author/([^/]+)/?$ http://newdomain.com/user/$1 [R=301,L]
RewriteCond %{HTTP_HOST} olddomain\.com [NC]
RewriteRule ^add/?$ http://newdomain.com/submit [R=301,L]

Have you tried using RedirectMatch?
In the htaccess file in your olddomain.com document root (it must be different from your newdomain.com root):
RedirectMatch 301 ^/author/(.*)$ http://newdomain.com/user/$1
RedirectMatch 301 ^/add(.*)$ http://newdomain.com/submit$1
Redirect 301 / http://newdomain.com/

Related

HTAccess Redirect specific URLs to new domain pages and then rest to Root

Having checked other questions and trying the suggested solutions, nothing has worked so far.
I'm trying to redirect certain URLs from the old-domain to URLs on the new-domain, not necessarily with the same page names. The rest of the URLs should be redirected to the root of the new-domain. This is what I've tried. The redirecting of all pages to the root of the new-domain works, just not the individual pages:
RewriteEngine on
Redirect 301 /travel/ferry.html http://www.new-domain.com/ferry/
RewriteEngine off
#
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?old-domain\.com$ [NC]
RewriteRule ^ http://www.new-domain.com/? [R=301,L]
Thank you.
Don't mix Redirect directive and RewriteRule directives as they come from different Apache modules and their order of execution might be unpredictable.
You may have your rules as this:
RewriteEngine on
# keep specific redirect here
RewriteRule ^travel/ferry\.html$ http://www.new-domain.com/ferry/ [L,NC,R=301]
# redirect rest of the URLs to root
RewriteCond %{HTTP_HOST} ^(?:www\.)?old-domain\.com$ [NC]
RewriteRule ^ http://www.new-domain.com/? [R=301,L]
Make sure to test it in a new browser or test after fully clearing browser cache.

How to redirect user to other link only when complete url matches

I want to redirect the user to other link only if the complete url matches and also is it possible that after redirection the url remains same as that of the old one from where user is redirected
I am writing the following rule
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !http://bestcenters.net/services$ [NC]
RewriteRule ^(.*)$ https://4677--586.rocketquotes.com/franchise [L,R=301]
Yes i have solved the issue. You just have to 301 redirect the new rule will looks like this
Redirect 301 ^/old-page.html$ http://newdomain/
This is the rule that helped me achieve what I wanted.

redirect old site with query to new domain with query

I'm looking for an htaccess rule to 301 redirect an old domain search result page, to a new domains result page keeping only the "query" parameter.
For example: change from http://myoldwebsite.com/search?query=free+templates&type=post&submit= to http://mynewwebsite.com/?s=free+templates.
I've found a method of redirecting each url manually, but I'm thinking there is an easier way to accomplish this using a single rule.
Can anyone point me in the right direction?
Put this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?myoldwebsite\.com$ [NC]
RewriteCond %{QUERY_STRING} (?:^|&)query=([^&]*)(?:&|$) [NC]
RewriteRule ^search/?$ http://mynewwebsite.com/?s=%1 [L,NE,R=301]

htaccess subdomain redirect to port while keeping original url

I have trouble getting this to work properly, what I'm trying to do is make
http://subdomain.domain.com redirect to domain.com:8080 while keeping the original url
"subdomain.domain.com"
Code so far:
RewriteEngine on
RewriteCond %{HTTP_HOST} subdomain.domain.com
RewriteRule ^(.*)$ http://%1domain.com:8080$1 [L]
Which does the redirect, but browser url changes to "http://domain.com:8080" which is not what I seek.
Thank you in advance!
For this to happen you need to enable mod_proxy in subdomain\.domain\.com. Once it is enabled try this rule in DocumentRoot/.htaccess of subdomain.domain.com:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^subdomain\.(domain\.com)$
RewriteRule ^ http://%1:8080%{REQUEST_URI} [L,NE,P]

unable to come up with htaccess rewrite engine

I want to redirect domain1.com to domain2.com in all cases except one particular case:
domain1.com/subfolder/index.php
I want this domain1.com/subfolder/index.php to be intact and not get redirected to domain2.com because I have hundreds of users already bookmarked this page.
But anything and everything besides that domain1.com/subfolder/index.php, I want domain1.com to be redirected to domain2.com
Please help.
Have the following .htaccess in your web root /
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)domain1.com$ [NC]
RewriteCond %{REQUEST_URI} !^/subfolder/index.php$ [NC]
RewriteRule ^(.*)$ http://domain2.com/$1 [R=301,L]
Use just [L] if you want the redirection to be transparent i.e. without letting your visitors know.
Use !^/subfolder/.*$ if you want to stop redirection for the complete folder as well as its contents.
You can use RewriteCond to check to see the requested uri is anything but the one you want to redirect, capture the desired path and then redirect to the second domain.
RewriteCond %{REQUEST_URI} !^/subfolder/index.php$
RewriteRule (.*) http://domain2.com/$1