htaccess Redirect Domain to Subdirectory - .htaccess

A client wants to redirect a domain (oldsite.com) to a new domain & subdirectory (newsite.com/subdirectory). I have this code setup and it works for for oldsite.com, but when I test oldsite.com/subdomain I get a 404 at newsite.com.
Redirect 301 / http://www.newsite.com/subdomain/
Is there a way to handle any subdomain requests at oldsite.com so they all redirect to newsite.com/subdirectory? Maybe a wildcard?

You can use this in the .htaccess of the web root of the old site:
RewriteEngine On
RewriteRule ^subdomain/(.*) http://newsite.com/subdirectory/$1 [B,NE,R,L]

Try this:
RewriteCond %{HTTP_HOST} oldsite\.com [NC]
RewriteRule (.*) http://newsite.com/subdirectory/$1 [QSA,L]

Related

.htaccess redirect all subfolders to http except main domain

Hello!
Please help me!
I have a main domain "domain.com"
And i have a subfolders
--folder_one
--folder_two
--folder_three
--folder_four
...
How can i, using, htaccess, redirect users from http://example.com to https://example.com and if user going to any subfolder in this domain redirect him to http of this, Example: https://example.com/folder_one to https://example.com/folder_one
I have done this, but i get a permanent redirect.
Please help.
Many thanks!
This redirect main domain to http and subfolders to https. I want exactly the opposite
RewriteCond %{HTTP_HOST} !^example\.com$ [NC,OR]
RewriteCond %{HTTPS} on
RewriteRule ^ http://example.com%{REQUEST_URI} [NE,R=301,L]

Getting subdomain to redirect and follow CNAME

i have migrated a domain from olddomain.com to newdomain.com...i want all URLs to redirect from olddomain.com to newdomain.com. I got this part working. My issue now is that I have a CNAME on newdomain for help.newdomain.com -> externaldomain.com (uses referrerURL to land on proper page)
I need to redirect help.olddomain.com -> help.newdomain.com -CNAME-> externaldomain.com
Is this possible with .htaccess rewrite? if not, is there another way to skin it?
Yes, a 301 .htaccess rewrite should do it. Put this in the server that currently serves help.olddomain.com:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^help.olddomain.com [NC]
RewriteRule ^(.*)$ http://externaldomain.com/$1 [L,R=301,NC]
If you really want it to hit help.newdomain.com first, use that in the RewriteRule instead.

.htaccess redirect for https://.co.nz website to .com version(including all subpages)

I am palling to migrate my website from the .co.nz to the .com but need to setup a 301 redirect so all of the individual pages will still be routed properly without any 404 pages from .co.nz version
All https://
I want "https://www.example.co.nz/" to go to "https://www.example.com"
Which will also redirect
https://www.example.co.nz/contact to go to https://www.example.com/contact
https://www.example.co.nz/men to go to https://www.example.com/men
Many Thanks
This should do the trick:
Redirect 301 / https://www.new-example.com/
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !new-example.com$ [NC]
RewriteRule ^(.*)$ https://new-example.com/$1 [L,R=301]
You put this in the .htaccess file of the .co.nz domain. This will redirect all your content and subfolders to their appropriate subfolders on the new domain (so example.me/subfolder to example.com/subfolder) AND also force redirect to https part.

htaccess file: Prevent redirecting addon domain pages

I have a main ssl domain https://www.a.com and a normal addon domain http://www.b.com on the same server. I use this .htaccess rule for a.com to redirect an old page in that website to a new one:
Redirect permanent /my_page.php /my_page
It works just fine. When i access a.com/my_page.php it redirects to a.com/my_page. The problem is that the addon domain b.com has the same page name inside its folder, so when i access b.com/my_page.php it also redirects to b.com/my_page. I need to prevent this redirection for the addon domain.
Note: I have tried to use this redirection rule instead but it didn't work at all:
RewriteRule https://www.a.com/my_page.php https://www.a.com/my_page
And this one too didn't work:
RedirectMatch 301 ^a.com/my_page.php$ https://www.a.com/my_page
After some reading and studying i was able to fix this using the RewriteRule instead. Here's the new code:
RewriteCond %{HTTP_HOST} ^.*a.com$ [NC]
RewriteCond %{REQUEST_URI} ^/my_page\.php$ [NC]
RewriteRule ^(.*)$ https://www.a.com/my_page [L,R=301]

Redirect URLs of Domain that is already 301 Redirected

I don't even know how to ask this question correctly, because I have never seen it done before. I am pretty competent with editing name servers, creating a records, and cnames etc, but I have no idea if this is even possible.
I have a client that owns a several domains that all point at the same site. So say the main domain is www.client-site.com. So then www.other-domain-1.com and www.other-domain-2.com are simply set as 301 redirects to point at www.client-site.com.
So all is good until now he is requesting that www.other-domain-1.com/facebook and www.other-domain-1.com/linkedin point to his facebook page or linkedin profiles instead of redirecting to the main domain.
The 301 redirect is happening at the registrar, and I don't believe there is a way to do what he wants from there. But I am thinking I could instead point it at the web host nameservers and include it as a addon domain, and then use the .htaccess file to do the 301 redirect of only the hostname, and then redirect as desired the hostname/paths.
So what is the proper way to do this? Something like...
Redirect 301 http://other-domain-1.com/facebook http://facebook.com/account
Redirect 301 http://other-domain-1.com/linkedin http://linkedin.com/profile
Redirect 301 http://other-domain-1.com http://client-site.com
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
# facebook redirect
RewriteCond %{HTTP_HOST} ^(www\.)?other-domain-1\.com$ [NC]
RewriteRule ^facebook/?$ http://facebook.com/account [L,NC,R=302]
# linked-in redirect
RewriteCond %{HTTP_HOST} ^(www\.)?other-domain-1\.com$ [NC]
RewriteRule ^linkedin/?$ http://linkedin.com/profile [L,NC,R=302]
# rest of the redirects
RewriteCond %{HTTP_HOST} ^(www\.)?other-domain-1\.com$ [NC]
RewriteRule ^ http://client-site.com%{REQUEST_URI} [L,NE,R=302]
So the Solution that worked on my server based on anubhava answer above was
facebook redirect
RewriteCond %{HTTP_HOST} ^(www\.)?other-domain-1\.com$ [NC]
RewriteRule ^facebook/?$ http://facebook.com/account [L,NC,R=302]
Then after getting them all working changed the R=302 to R=301 to make the redirects permanent.

Resources