.htaccess 301 redirect all pages on old domain to a single page on new domain - apache

I'm looking to redirect each and every page on the old domain to a single page on the new domain. It should also redirect both the www and non-www versions. Every single request to the domain old.com, whatever it may be, should lead to www.new.com root.
old.com/1.php
www.old.com/2.php
old.com/2.php
old.com/links/index.php
old.com/about-us.html
old.com/?string=1
should all redirect to:
www.new.com/
I'm looking for a .htaccess solution.

You can use RedirectMatch in the old.com vhost
RedirectMatch permanent .* http://www.new.com/
Redirect appends the trailing portion of the matched URI to the redirection URI but with RedirectMatch it's up to you to choose the parts (if any) that you want to transfer using parentheses and $number references.
If the old and new domains must be aliases for the same document root on disk then you'll probably have to use mod_rewrite
RewriteEngine on
RewriteCond %{HTTP_HOST} old\.com$
RewriteRule .* http://www.new.com/ [L,R=301]

This has already been answered, but here's the code altogether containing helpful comments so people have a reference to look back on later should they forget what it does.
Put this in your .htaccess file:
## Each and every page on old domain redirects to single page
## By appending question mark to new domain, query strings are removed
RewriteEngine on
RewriteCond %{HTTP_HOST} old\.com$
RewriteRule .* http://www.new.com/? [L,R=301]
You can also redirect to a sub directory on another domain, as was the case that I needed to do like this:
## Each and every page on old domain redirects to single page
## By appending question mark to new domain, query strings are removed
RewriteEngine on
RewriteCond %{HTTP_HOST} old\.com$
RewriteRule .* http://www.new.com/sub-directory/? [L,R=301]

This will handle all your redirects including query strings. Excuse the formatting, I'm on a phone :)
RewriteCond %{HTTP_HOST} old\.com$ RewriteCond %{QUERY_STRING} .+
RewriteRule (.*) http://www.new.com$1?%{QUERY_STRING} [L,R=301,QSA]
RewriteCond %{HTTP_HOST} old\.com$
RewriteRule (.*) http://www.new.com$1 [L,R=301,QSA]

Related

301 Issues with 2 URLs on same webspace

I have 2 domains, the old domain is mjvandco.co.uk and he wants this redirecting to mjvlaw.co.uk. I have both pointing to the same webspace but when I test the URLs using https://httpstatus.io/ I get different results.
I have the following in my htaccess along with other stuff, but this is the redirect content:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.mjvlaw\.co\.uk
RewriteRule (.*) https://www.mjvlaw.co.uk/$1 [R=301,L]
# Remove .html (excluding blog)
RewriteCond %{REQUEST_URI} !^/blog(.*)$
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.html\ HTTP/
RewriteRule ^(.*)\.html$ /$1 [R=301,L]
The only URL that now is not right is this one: http://www.mjvlaw.co.uk/. I used this site
https://mjvlaw.co.uk - goes to https://www.mjvlaw.co.uk
http://mjvlaw.co.uk - goes to https://www.mjvlaw.co.uk
http://www.mjvlaw.co.uk - not work as does not go to https
https://www.mjvlaw.co.uk - fine
However, when I do the same for the old domain it all works as it should and every one below goes too https://www.mjvlaw.co.uk.
https://www.mjvandco.co.uk
https://mjvandco.co.uk
http://mjvandco.co.uk
http://www.mjvandco.co.uk
Am I doing something stupid here? Should I create another webspace and have one folder for the old domain and what for the current one and each having it's own htaccess file?
Thanks. I have done another ticket a month or so back but I am not sure how to change the questions, so I apologise for the similar ticket.
You rule only redirects non-www http URLs to SSL version of your site. To redirect both non-www and www http versions , replace your first rewrite block with the following
RewriteCond ℅{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.mjvlaw\.co\.uk
RewriteRule (.*) https://www.mjvlaw.co.uk/$1 [R=301,L]
Make sure to clear your browser cache before you test this.

Redirect all pages in direcory except one subdirectory .htaccess

I want to have one URL still being served from a subdirectory on the old domain but redirect all others from the parent directory to the new domain.
www.olddomain.co.uk/directorya/directoryb -> no redirect
www.olddomain.co.uk/directorya -> www.newdomain.co.uk/directorya
This works if the URL is typed with a trailing slash
RewriteCond %{REQUEST_URI} !^/directorya\/directoryb [NC]
RewriteRule ^(.*)$ http://www.newdomain.co.uk/ [L,R=301]
So http://www.olddomain.co.uk/directorya/directoryb/ works but http://www.olddomain.co.uk/directorya/directoryb fails.
Tried added $ at the end and other connotations and tried
RewriteEngine on
RewriteRule !^myspecialdirectory/mynextdirectory($|/) http://newdomain.example%{REQUEST_URI} [L,R=301]
from Redirect entire site except one directory to a new site - apache .htaccess
What do I need to do to get http://www.olddomain.co.uk/directorya/directoryb/ or http://www.olddomain.co.uk/directorya/directoryb to redirect? (while all other http://www.olddomain.co.uk pages go to http://www.newdomain.co.uk
For reasons I don't understand http://www.olddomain.co.uk/directorya/directoryb goes to Google and doesn't redirect
Try using THE_REQUEST variable and make sure this this very first rule:
RewriteCond %{THE_REQUEST} !\s+/+directorya/directoryb [NC]
RewriteRule ^ http://www.newdomain.co.uk/? [L,R=301]
Test it after clearing your browser cache.

How to redirect from domain to subdomain?

My site is www.mysite.com and I need to redirect any request to us.mysite.com.
So:
www.mysite.com ----> us.mysite.com
www.mysite.com/hello.php ----> us.mysite.com/hello.php
// etc
I tryed this but doesn't work:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mysite.com$
RewriteRule (.*) http://us.mysite.com$1 [R=301]
It looks like your RewriteCond is only matching domains that start and end with mysite.com. This does not include www.mysite.com.
The following will 301 redirect anything NOT at us.mysite.com to us.mysite.com:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^us.mysite.com$
RewriteRule ^(.*)$ http://us.mysite.com/$1 [R=301]
There are several different solutions. The best one, both from SEO and User perspective, is the one-to-one 301 redirect. It preserves your link juice and at the same time redirects the client to the exact location on the new website.
If you have mod_alias enabled, I would suggest a simple
RedirectMatch 301 ^(.*)$ / http://new.domain.com/$1
The result instruction can be accomplished with
RewriteEngine On
RewriteRule (.*) http://new.domain.com/$1 [R=301,L]
The second one is the best choice if you need to chain multiple conditions and filters. For example, if you need to redirect only certain hosts or clients depending on User Agent header.
From here.

How to redirect dynamic pages using htaccess

I have moved site from one framework to another and now I would like to redirect important pages (around 20). How to redirect them using apache rewrite? Here are examples:
Old: http://mydomain.com/ba/stream.php?kat=15
New: http://www.mydomain.com/bs/about-us/our-partners
Old: http://mydomain.com/ba/stream.php?kat=29
New: http://www.mydomain.com/bs/catalogues/it-catalogue
Also, after important pages are listed, I'd like to redirect all remaining links in form:
http://mydomain.com/ba/whatever-is-here
to
http://www.mydomain.com/bs/
Following TerryE's suggestion, I'll attach my latest code which is not working :)
RewriteCond %{QUERY_STRING} ^kat=15$
RewriteRule ^stream\.php$ http://www.mydomain.com/bs/about-us/our-partners [R=301,L]
One more note: I use this code to redirect root domain to www subdomain:
RewriteCond %{HTTP_HOST} ^mydomain\.com$ [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301,L]
I have placed this root to www code below code for specific pages redirection, and when I test http://mydomain.com/ba/stream.php?kat=15 in browser, I am redirected to http://www.mydomain.com/bs/ba/stream.php?kat=15
Adnan,
You need to add the following lines to your DOCROOT/.htaccess. This assumes that you don't have any .htaccess file in your ba subdirectory. (See Tips for debugging .htaccess rewrite for an explanation of why.)
RewriteEngine On
RewriteBase /
#
# Redirect kat 15 to the Partners page
#
RewriteCond %{QUERY_STRING} ^kat=15$
RewriteRule ^ba/stream\.php$ http://www.mydomain.com/bs/about-us/our-partners? [R=301,L]
#
# Redirect all other kat ids to the corresponding bs page
#
RewriteCond %{QUERY_STRING} ^kat=(\d+)$
RewriteRule ^ba/stream\.php$ http://www.mydomain.com/bs/%1? [R=301,L]
Note:
the rule matches against the UPI less the leading path (in this case /)
you need to have the trailing ? on the replacement string to suppress the existing query parameters.
the %n parameters pick up the match variables from the last successful condition match.

301 redirect .htaccess

Im trying to request the following entire site 301 redirect:
word.something.blah.domain.com --> http://www.word.com
I don't know how to write the 301 redirect rule.
Can someone help out?
I will assume you are using the same directory to serve files on both domains. In which case, a Redirect clause won't work (infinite redirect loop).
With mod_rewrite, you can check the value of the current HTTP_HOST and take a decision based on that:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^([a-zA-Z0-9]+)\.something\.blah\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.%1.com/$1 [R=301,NE,L]
put this into root directory of the subdomain:
Redirect permanent / http://www.word.com
If you are keeping everything else the same - that is, the file names - but simply changing the domain, this code is all you need to put on the OLD DOMAIN htaccess:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.newdomain\.co.uk
RewriteRule (.*) http://www.newdomain.co.uk/$1 [R=301,L]