.htaccess redirect one domain to other - wordpress

I have wordpress multisite with 2 sites: https://www.example1.com/ AND https://www.example2.com/
example2 site is a copy of example1, but 2 is new so I want to redirect all subsites to new domain.
Example: https://www.example1.com/data/post1243545 to https://www.example2.com/data/post1243545
Everything stays the same except domain

To answer the question as asked
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^olddomain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.olddomain.com$
RewriteRule (.*)$ http://www.newdomain.com/$1 [R=301,L]
</IfModule>
per https://wpscholar.com/blog/redirect-old-domain-to-new-domain-via-htaccess/
However, you could simply configure the sites to use different domains and avoid having to use htaccess entirely as described in the following link.
https://wordpress.org/support/topic/change-domain-in-multisite/

Related

301 Redirect from non-existent subdomain to subfolder ( closed wordpress multi-site )

About to change a wordpress multi-site with 100+ locations to a standard wordpress site and I'd like to redirect the old closed subdomains to the new specific subfolders relating to those locations.
Because wordpress creates the subdomains dynamically I'm trying to work out if there is a easy way to 301 redirect from the now non-existent subdomains to the new subfolders via htaccess...
https://example1.domain.com.au > https://www.domain.com.au/office-locations/example1
https://example2.domain.com.au > https://www.domain.com.au/office-locations/example2
I'd appreciate any help I've tried a few simple redirect lines like the following with no luck just yet. And i'm not even sure if the .htaccess file in the root folder is best or the one in the main domains public html folder.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example1.domain.com.au$ [NC]
RewriteRule (.*) http://www.domain.com.au/locations/example1 [R=301,L]
RewriteCond %{HTTP_HOST} ^example2.domain.com.au$ [NC]
RewriteRule (.*) http://www.domain.com.au/locations/example2 [R=301,L]
Cheers and hope this makes sense
Try this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example1.domain.com.au$
RewriteRule ^(.*)$ http://www\.domain\.com\.au/example1/$1 [R=301,L]

Using .htaccess to point primary domain to subfolder without altering the URL

I have a domain i.e www.domain.org and want to point it to a subfolder on my server. The main domain loads from /public_html and now, I want to load the domain from /public_html/domain2017.
I have researched various solutions on the web, but all I get is a redirect.
The redirect is not preferred because I end up with www.domain.org/domain2017 when I want to stay as www.domain.org but load the index.php file of www.domain.org/domain2017. Hopefully, this makes sense. Note: I have a WordPress installation within public_html/domain2017.
Below is example content of my .htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.org$ [NC]
RewriteRule !^subdir/ /domain2017%{REQUEST_URI} [L,NC]
</IfModule>
Solution Attempt 1 Solution Attempt 2 Solution Attempt 3 Solution Attempt 4
I have tried all of the above links and they simply redirect my domain to the URL and does not prevent the URL in the address bar from changing
You can use the following htaccess:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^yourdomain.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.yourdomain.com$
RewriteCond %{REQUEST_URI} !public_html/domain2017/
RewriteRule (.*) /public_html/domain2017/$1 [L]

.htaccess file exact match and not exact match

I just moved to a WordPress site. I used to have a static site and a WordPress blog on a subdomain (blog.example.com). I'm using the code below, which is redirecting all of my blog posts to my new website perfectly fine. Is there any way to make an exception for the main blog page? To be specific, I need the code below to apply to all my posts, categories, etc. (which it already is), but I need my old blog home page (blog.example.com) to 301 redirect to my new blog homepage (example.com/blog). Just to be very clear, my posts, etc. are NOT using blog in the permalink.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^blog.example.com$ [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
</IfModule>
UPDATE: Thanks, Olaf! Got this working with your advice. Here is my final code for anyone else looking for an answer.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^blog.example.com$ [NC]
RewriteRule ^$ http://www.example.com/blog [R=301,L]
RewriteCond %{HTTP_HOST} ^blog.example.com$ [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
</IfModule>
Put the most specific rules at the beginning. In your case, prepend the existing rule with the redirect for the main page
RewriteCond %{HTTP_HOST} ^blog.example.com$ [NC]
RewriteRule ^$ http://www.example.com/blog [R,L]
When everything works as it should, you may replace R with R=301. Never test with R=301.

Wordpress - moving a site to another domain, 301 redirects

I moved a wordpress site to another domain. I want to preserve the google links however and help it re-index the new site.
I did a 301 redirect but it only works for the root director. When someone goes to http://oldsite.com/directory/post-name-here/ I want them to be forwarded to http://newsite.com/directory2/post-name-here/. Both websites are in subdirectories. Is there a way to do it with in PHP? Or would I need a mod-rewrite?
What finally worked for me was this:
My oldsite file structure is like so:
/public_html/
index.php
/development/
.htaccess
To recap, I wanted to forward everything coming into the development directory to the new website. This is the code of the .htaccess in the development directory.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^oldsite.org$ [OR]
RewriteCond %{HTTP_HOST} ^www.oldsite.org$
RewriteRule (.*)$ http://newsite.com/newdirectory/$1 [R=301,L]
Thank you so much to Peter
This has always worked for me:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^olddomain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.olddomain.com$
RewriteRule (.*)$ http://www.newdomain.com/$1 [R=301,L]
You have to add this rule to .htaccess file see below.
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^OLDDOMAIN\.com$ [NC]
RewriteRule ^(.*)$ http://NEWDOMAIN.com [R=301,L]
If you have only one link under the directory. You can add a index.php file to http://oldsite.com/directory/ with this code.
header("location: http://newsite.com/directory2/post-name-here/", true, 301);
If you have many links under the directory, then use the following code
if($_SERVER["REQUEST_URI"]=="/directory/post-name-here/"){
header("location: http://newsite.com/directory2/post-name-here/", true, 301);
}
Here, we check if the file name is the old link, then redirect to new domain.

Drupal Multisite in subfolders, www redirection in htaccess

I'm using a multisite setup in subfolders, everything works fine except the non-www to www redirection.
I got it working with no issues for the main domain, but I cant get it to work for the other sites:
for example , I want this redirection :
site.com/ru > www.site.com/ru
Try
Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^site.com [nc]
rewriterule ^(.*)$ http://www.site.com/$1 [r=301,nc]
If you check .htaccess file you may see on 85-90 line (Drupal 7.x):
# To redirect all users to access the site WITH the 'www.' prefix,
# (http://example.com/... will be redirected to http://www.example.com/...)
# uncomment the following:
# RewriteCond %{HTTP_HOST} .
# RewriteCond %{HTTP_HOST} !^www\. [NC]
# RewriteRule ^ http%{ENV:protossl}://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
And if you uncomment this lines you may get solutions for your problem.
RedirectPermanent /ru http://www.site.com/ru
might be a better alternative to Rewrite rules.

Resources