htaccess file: Prevent redirecting addon domain pages - .htaccess

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]

Related

.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 redirect folder to subdomain

I've tried applying a few of the answers found on stackoverflow, but either I'm missing something or I'm plain dumb.
Basically I got a main domain name. This domain already has a non-www redirect. So http://domain.com becomes http://www.domain.com. This domain also has a mobile version found inside the the 'm' folder. So accessing the domain name like http://www.domain.com/m/ works and so does http://m.domain.com. What I'm trying to achieve is simple: anyone whom goes to the site via http://www.domain.com/m/, or http://www.domain.com/m/about should be redirected to the subdomain version so to http://m.domain.com or http://m.domain.com/about in the second case listed above.
Whatever I tried implementing ended up with errors, either I managed to disable direct access to m.domain.com, but it worked via domain.com/m/, or redirect loops.
Thanks!
You can use this code in your DOCUMENT_ROOT/.htaccess file of domain.com main .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?(domain\.com)$ [NC]
RewriteRule ^m/(.*)$ http://m.%1/$1 [L,NC,R=302]
# non-www to www
RewriteCond %{HTTP_HOST} !^(m|www)\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,NC,R=302]

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.

htaccess Redirect Domain to Subdirectory

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]

Redirect all sub-domain to parent domain except one

I am able to access my website with random sub-domains like this
abcd.theonlytutorials.com
abc.theonlytutorials.com
etc.theonlytutorials.com/otherpage/otherpage
I have one real sub-domain where my blog is
blog.theonlytutorials.com
Is there any possible way to redirect all my fake sub-domains to root domain except the 'blog' one?
Try adding these rules to the htaccess file in your document root, preferably above any rules that you may already have in there:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^([^.]+)\.theonlytutorials\.com$ [NC]
RewriteCond %1 !^(blog|www)$ [NC]
RewriteRule ^(.*)$ http://theonlytutorials.com/$1 [L,R=301]
You may look into following URL:
Redirect sub domain to main domain

Resources