htaccess redirect with exceptions - wordpress

I work on a Wordpress Network. Let's say that the main site has the link www.main-site-old.com, and all the other sites in this network have links like www.main-site-old.com/site1 , www.main-site-old.com/site2 e.t.c.
I want to redirect ONLY the main site to a new domain (let's say www.main-site-new.com) and let all the others stay as they are.
I tried this one:
Redirect 301 / http://www.main-site-new.com/
but it redirects the whole domain (for example it redirects www.main-site-old.com/site1 to www.main-site-new.com/site1)
I also tried:
RewriteCond %{REQUEST_URI} !=/site1
RewriteRule ^.*$ http://main-site-new.com/$0 [R=301,L]
but nothing happened.

With mod_alias redirect you will not be able to use regex to capture a request for http://www.main-site-old.com only so, you could use another one RedirectMatch like this :
RedirectMatch 301 ^/?$ http://www.main-site-new.com
Or go to mod_rewrite by this :
RewriteEngine On
RewriteRule ^/?$ http://www.main-site-new.com [R=301,L]
Note: clear browser cache then test.

Related

How to redirect http to specific URL using htaccess in WordPress

I am looking for assistance with the redirection rules.
I have url www.example.com.au and I want the following redirections
https://www.example.com.au =>https://www.example.com.au
http://www.example.com.au =>https://www.example.com.au/page-2
I found the following .htaccess rules but its not working for me
RewriteEngine On
RewriteCond %{HTTP_HOST} www^example\.com.au [NC]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%.
{HTTP_HOST}%www^example.com.au/page-2 [302 L]
can anyone please help me the correct rule.
I will be using the above rule for a WordPress site so do i need to paste the above rules at the bottom so that it doesn't overruled by other rewrite rule?
Thanks in advance.
For the first redirection it is impossible, it will create an infinite loop. You can not redirect an url on itself.
And for the second one ( http://www.example.com.au => https://www.example.com.au/page-2 ) you can do something like that
Redirect 301 / https://www.example.com.au/page-2
Pay attention to 301, it indicates that the redirection is permanent, I do not know if it is your goal.
But if your goals was to redirect http to https you can do something like that (put on the top of your .htaccess file):
RewriteEngine on
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule (.*) https://www.example.com.au/$1 [R=301,L]
Or use a plugin like Really Simple SSL
Then you can define your rules to redirect one page to another before the rule above, like this:
Redirect 301 /old-page /new-page

Redirect subdomain and path to main domain with same path

I have recently stopped using a subdomain for my blog, i need to forward all links that use that domain to the same link path but on my main domain.
Example being:
blog.example.com/blog/sales/blog-title
needs to redirect to :
www.example.com/blog/sales/blog-title
I can redirect the subdomain itself so
-blog.example.com/
goes to
www.example.com/blog
but as soon as I try to redirect a page it doesn't redirect and doesn't load. Can anyone shed some light on it? I currently have this in my htaccess for my main redirect:
RewriteCond %{HTTP_HOST} ^blog\.example\.com [NC]
RewriteRule (.*) http://www.example.com/blog/$1 [L,R=301]
I have other subdomain redirects going on but none wildcard redirect.
In your example, you want to redirect from
blog.example.com/blog/sales/blog-title
to
www.example.com/blog/sales/blog-title
But in your rule, you insert another subdirectory blog in the substitution part, which gives
www.example.com/blog/blog/sales/blog-title
instead, and an error 404 as a result.
To redirect from one domain to another with the exact same request path, use
RewriteCond %{HTTP_HOST} ^blog\.example\.com$ [NC]
RewriteRule ^ http://www.example.com%{REQUEST_URI} [L,R]
When it works as it should, you may replace R with R=301. Never test with R=301.

.htaccess to change the domain in the URL

I have the following two WordPress sites.
test.mysite.com
mysite.com
The uploads(images) folder is on mysite.com and I want all image URLs from test.mysite.com to be redirected to mysite.com
For example the link test.mysite.com/wp-content/uploads/2015/09/image.jpg should be redirected to mysite.com/wp-content/uploads/2015/09/image.jpg
I appreciate any help.
If you care about your website traffic and SEO. I would recommend using 301 redirection which is: RedirectMatch permanent
It's quite easy! Make an new .htaccess file at test.mysite.com > Just add this chunk of codes bellow:
RewriteEngine On
RedirectMatch permanent ^/wp-content/uploads/(.*)$ http://mysites.com/wp-content/uploads/$1
Now all images Should be 301 SEO redirected to the concerned location.
You can place this redirect rule just below RewriteBase line:
RewriteCond %{HTTP_HOST} ^test\.(mysite\.com)$ [NC]
RewriteRule ^(wp-content/uploads/.+)$ http://%1/$1 [L,NC,R=301]

How to redirect URL containing question mark to different domain with .htaccess

It looks like this is a common issue since there are so many related questions here but I still have not found the solution after hours of searching...
I need to do many redirects via .htaccess for an old WordPress site. It uses URL's like this:
http://oldwpsite.com/?p=11
http://oldwpsite.com/?p=12
I need to redirect them something like this:
redirect 301 /?p=11 http://newwpsite.com/renting-a-scooter/ <--Does not work
redirect 301 /?p=12 http://newwpsite.com/scooter-tips/ <--Does not work
I have confirmed redirect does work with:
redirect 301 /xx.html http://newwpsite.com/ <--- Works
I tried many variations of RewriteEngine On, RewriteCond and RewriteRule but failed every time.
How can I do a redirect with question mark like the above?
This should redirect the two pages to where you want them to go:
RewriteEngine on
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /\?p=11\ HTTP
RewriteRule ^ http://newwpsite.com/renting-a-scooter? [R=301,L]
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /\?p=12\ HTTP
RewriteRule ^ http://newwpsite.com/scooter-tips? [R=301,L]

Using .htaccess to redirect a domain to another URL

I have a site running wordpress, it's the full site. One of the pages is like a contact-us form located at www.ourdomain.com/contact-us/
I also have a URL like contactourdomain.com and I want it to redirect to www.ourdomain.com/contact-us/
We used to do this with a redirect on network solutions, but I prefer to have it all done right on the server if possible. I've got it sort of working but when you visit the link is still says contactourdomain.com/contact-us/ as the URL, and that breaks all the other ones.
Any suggestions?
.htaccess
Options -MultiViews
RewriteEngine on
RewriteBase /
# Rewrite
RewriteRule ^(.*)$ http://www.ourdomain.com/contact-us//$1 [L]
if you add this in .htaccess is it working?:
RewriteRule ^$ contact-us/ [R=301,L]
keep me posted..

Resources